refactored. changed license to GPLv2 only. clean up console UI.

pull/40/head
Matchman Green 2012-04-26 12:21:40 +08:00
parent 947664937a
commit 41033f9bd5
12 changed files with 66 additions and 46 deletions

View File

@ -7,10 +7,10 @@ include(FindOpenSSL)
add_executable( grive add_executable( grive
src/main.cc src/main.cc
src/OAuth2.cc src/OAuth2.cc
src/HTTP.cc
src/Json.cc
src/Drive.cc src/Drive.cc
src/Download.cc ) src/protocol/HTTP.cc
src/protocol/Json.cc
src/protocol/Download.cc )
target_link_libraries( grive target_link_libraries( grive
curl curl

View File

@ -4,8 +4,8 @@
This program is free software; you can redistribute it and/or This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2 as published by the Free Software Foundation; version 2
of the License, or (at your option) any later version. of the License.
This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
@ -19,8 +19,8 @@
#include "Drive.hh" #include "Drive.hh"
#include "HTTP.hh" #include "protocol/HTTP.hh"
#include "Json.hh" #include "protocol/Json.hh"
#include "OAuth2.hh" #include "OAuth2.hh"
#include <openssl/evp.h> #include <openssl/evp.h>

View File

@ -5,7 +5,7 @@
This program is free software; you can redistribute it and/or This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2 as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version. of the License.
This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of

View File

@ -4,8 +4,8 @@
This program is free software; you can redistribute it and/or This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2 as published by the Free Software Foundation; version 2
of the License, or (at your option) any later version. of the License.
This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
@ -18,8 +18,9 @@
*/ */
#include "OAuth2.hh" #include "OAuth2.hh"
#include "HTTP.hh"
#include "Json.hh" #include "protocol/HTTP.hh"
#include "protocol/Json.hh"
// for debugging // for debugging
#include <iostream> #include <iostream>
@ -27,25 +28,32 @@
namespace gr { namespace gr {
const std::string token_url = "https://accounts.google.com/o/oauth2/token" ; const std::string token_url = "https://accounts.google.com/o/oauth2/token" ;
const std::string client_id = "22314510474.apps.googleusercontent.com" ;
const std::string client_secret = "bl4ufi89h-9MkFlypcI7R785" ;
OAuth2::OAuth2( const std::string& refresh_code ) : OAuth2::OAuth2(
m_refresh( refresh_code ) const std::string& refresh_code,
const std::string& client_id,
const std::string& client_secret ) :
m_refresh( refresh_code ),
m_client_id( client_id ),
m_client_secret( client_secret )
{ {
Refresh( ) ; Refresh( ) ;
} }
OAuth2::OAuth2( ) OAuth2::OAuth2(
const std::string& client_id,
const std::string& client_secret ) :
m_client_id( client_id ),
m_client_secret( client_secret )
{ {
} }
void OAuth2::Auth( const std::string& auth_code ) void OAuth2::Auth( const std::string& auth_code )
{ {
std::string post = std::string post =
"code=" + auth_code + "code=" + auth_code +
"&client_id=" + client_id + "&client_id=" + m_client_id +
"&client_secret=" + client_secret + "&client_secret=" + m_client_secret +
"&redirect_uri=" + "urn:ietf:wg:oauth:2.0:oob" + "&redirect_uri=" + "urn:ietf:wg:oauth:2.0:oob" +
"&grant_type=authorization_code" ; "&grant_type=authorization_code" ;
@ -74,8 +82,8 @@ void OAuth2::Refresh( )
{ {
std::string post = std::string post =
"refresh_token=" + m_refresh + "refresh_token=" + m_refresh +
"&client_id=" + client_id + "&client_id=" + m_client_id +
"&client_secret=" + client_secret + "&client_secret=" + m_client_secret +
"&grant_type=refresh_token" ; "&grant_type=refresh_token" ;
Json resp = Json::Parse( HttpPostData( token_url, post ) ) ; Json resp = Json::Parse( HttpPostData( token_url, post ) ) ;

View File

@ -4,8 +4,8 @@
This program is free software; you can redistribute it and/or This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2 as published by the Free Software Foundation; version 2
of the License, or (at your option) any later version. of the License.
This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
@ -26,8 +26,13 @@ namespace gr {
class OAuth2 class OAuth2
{ {
public : public :
OAuth2( ) ; OAuth2(
explicit OAuth2( const std::string& refresh_code ) ; const std::string& client_id,
const std::string& client_secret ) ;
OAuth2(
const std::string& refresh_code,
const std::string& client_id,
const std::string& client_secret ) ;
std::string Str() const ; std::string Str() const ;
@ -47,6 +52,9 @@ public :
private : private :
std::string m_access ; std::string m_access ;
std::string m_refresh ; std::string m_refresh ;
const std::string m_client_id ;
const std::string m_client_secret ;
} ; } ;
} // end of namespace } // end of namespace

View File

@ -4,8 +4,8 @@
This program is free software; you can redistribute it and/or This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2 as published by the Free Software Foundation; version 2
of the License, or (at your option) any later version. of the License.
This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
@ -19,7 +19,7 @@
#include "OAuth2.hh" #include "OAuth2.hh"
#include "Drive.hh" #include "Drive.hh"
#include "Json.hh" #include "protocol/Json.hh"
#include <cassert> #include <cassert>
#include <cstdlib> #include <cstdlib>
@ -27,6 +27,9 @@
#include <iostream> #include <iostream>
#include <iterator> #include <iterator>
const std::string client_id = "22314510474.apps.googleusercontent.com" ;
const std::string client_secret = "bl4ufi89h-9MkFlypcI7R785" ;
namespace gr namespace gr
{ {
const std::string& ConfigFilename() const std::string& ConfigFilename()
@ -75,19 +78,20 @@ int main( int argc, char **argv )
case 'a' : case 'a' :
{ {
std::cout std::cout
<< "Please go to this URL and enter the code:\n" << "-----------------------\n"
<< OAuth2::MakeAuthURL( "22314510474.apps.googleusercontent.com" ) << "Please go to this URL and get an authenication code:\n\n"
<< OAuth2::MakeAuthURL( client_id )
<< std::endl ; << std::endl ;
std::cout
<< "\n-----------------------\n"
<< "Please input the authenication code here" << std::endl ;
std::string code ; std::string code ;
std::cin >> code ; std::cin >> code ;
OAuth2 token ; OAuth2 token( client_id, client_secret ) ;
token.Auth( code ) ; token.Auth( code ) ;
// print the refresh token an exist
std::cout << "got refresh token: " << token.RefreshToken() << std::endl ;
// save to config // save to config
config.Add( "refresh_token", Json( token.RefreshToken() ) ) ; config.Add( "refresh_token", Json( token.RefreshToken() ) ) ;
assert( config["refresh_token"].As<std::string>() == token.RefreshToken() ) ; assert( config["refresh_token"].As<std::string>() == token.RefreshToken() ) ;
@ -98,7 +102,7 @@ int main( int argc, char **argv )
} }
} }
OAuth2 token( config["refresh_token"].As<std::string>() ) ; OAuth2 token( config["refresh_token"].As<std::string>(), client_id, client_secret ) ;
Drive drive( token ) ; Drive drive( token ) ;
return 0 ; return 0 ;

View File

@ -4,8 +4,8 @@
This program is free software; you can redistribute it and/or This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2 as published by the Free Software Foundation; version 2
of the License, or (at your option) any later version. of the License.
This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of

View File

@ -4,8 +4,8 @@
This program is free software; you can redistribute it and/or This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2 as published by the Free Software Foundation; version 2
of the License, or (at your option) any later version. of the License.
This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of

View File

@ -4,8 +4,8 @@
This program is free software; you can redistribute it and/or This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2 as published by the Free Software Foundation; version 2
of the License, or (at your option) any later version. of the License.
This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of

View File

@ -4,8 +4,8 @@
This program is free software; you can redistribute it and/or This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2 as published by the Free Software Foundation; version 2
of the License, or (at your option) any later version. of the License.
This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of

View File

@ -4,8 +4,8 @@
This program is free software; you can redistribute it and/or This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2 as published by the Free Software Foundation; version 2
of the License, or (at your option) any later version. of the License.
This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of