diff --git a/CMakeLists.txt b/CMakeLists.txt index 5a4f926..0e5c45e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,10 +7,10 @@ include(FindOpenSSL) add_executable( grive src/main.cc src/OAuth2.cc - src/HTTP.cc - src/Json.cc src/Drive.cc - src/Download.cc ) + src/protocol/HTTP.cc + src/protocol/Json.cc + src/protocol/Download.cc ) target_link_libraries( grive curl diff --git a/src/Drive.cc b/src/Drive.cc index ec80580..2ba8793 100644 --- a/src/Drive.cc +++ b/src/Drive.cc @@ -4,8 +4,8 @@ This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License - as published by the Free Software Foundation; either version 2 - of the License, or (at your option) any later version. + as published by the Free Software Foundation; version 2 + of the License. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of @@ -19,8 +19,8 @@ #include "Drive.hh" -#include "HTTP.hh" -#include "Json.hh" +#include "protocol/HTTP.hh" +#include "protocol/Json.hh" #include "OAuth2.hh" #include diff --git a/src/Drive.hh b/src/Drive.hh index ae614ee..2cc8cbb 100644 --- a/src/Drive.hh +++ b/src/Drive.hh @@ -5,7 +5,7 @@ This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License 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, but WITHOUT ANY WARRANTY; without even the implied warranty of diff --git a/src/OAuth2.cc b/src/OAuth2.cc index 71b0ae5..c9095d4 100644 --- a/src/OAuth2.cc +++ b/src/OAuth2.cc @@ -4,8 +4,8 @@ This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License - as published by the Free Software Foundation; either version 2 - of the License, or (at your option) any later version. + as published by the Free Software Foundation; version 2 + of the License. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of @@ -18,8 +18,9 @@ */ #include "OAuth2.hh" -#include "HTTP.hh" -#include "Json.hh" + +#include "protocol/HTTP.hh" +#include "protocol/Json.hh" // for debugging #include @@ -27,25 +28,32 @@ namespace gr { 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 ) : - m_refresh( refresh_code ) +OAuth2::OAuth2( + 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( ) ; } -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 = "code=" + auth_code + - "&client_id=" + client_id + - "&client_secret=" + client_secret + + "&client_id=" + m_client_id + + "&client_secret=" + m_client_secret + "&redirect_uri=" + "urn:ietf:wg:oauth:2.0:oob" + "&grant_type=authorization_code" ; @@ -74,8 +82,8 @@ void OAuth2::Refresh( ) { std::string post = "refresh_token=" + m_refresh + - "&client_id=" + client_id + - "&client_secret=" + client_secret + + "&client_id=" + m_client_id + + "&client_secret=" + m_client_secret + "&grant_type=refresh_token" ; Json resp = Json::Parse( HttpPostData( token_url, post ) ) ; diff --git a/src/OAuth2.hh b/src/OAuth2.hh index 0403c40..4666ec9 100644 --- a/src/OAuth2.hh +++ b/src/OAuth2.hh @@ -4,8 +4,8 @@ This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License - as published by the Free Software Foundation; either version 2 - of the License, or (at your option) any later version. + as published by the Free Software Foundation; version 2 + of the License. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of @@ -26,8 +26,13 @@ namespace gr { class OAuth2 { public : - OAuth2( ) ; - explicit OAuth2( const std::string& refresh_code ) ; + OAuth2( + 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 ; @@ -47,6 +52,9 @@ public : private : std::string m_access ; std::string m_refresh ; + + const std::string m_client_id ; + const std::string m_client_secret ; } ; } // end of namespace diff --git a/src/main.cc b/src/main.cc index 37822f0..e013b3d 100644 --- a/src/main.cc +++ b/src/main.cc @@ -4,8 +4,8 @@ This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License - as published by the Free Software Foundation; either version 2 - of the License, or (at your option) any later version. + as published by the Free Software Foundation; version 2 + of the License. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of @@ -19,7 +19,7 @@ #include "OAuth2.hh" #include "Drive.hh" -#include "Json.hh" +#include "protocol/Json.hh" #include #include @@ -27,6 +27,9 @@ #include #include +const std::string client_id = "22314510474.apps.googleusercontent.com" ; +const std::string client_secret = "bl4ufi89h-9MkFlypcI7R785" ; + namespace gr { const std::string& ConfigFilename() @@ -75,19 +78,20 @@ int main( int argc, char **argv ) case 'a' : { std::cout - << "Please go to this URL and enter the code:\n" - << OAuth2::MakeAuthURL( "22314510474.apps.googleusercontent.com" ) + << "-----------------------\n" + << "Please go to this URL and get an authenication code:\n\n" + << OAuth2::MakeAuthURL( client_id ) << std::endl ; + std::cout + << "\n-----------------------\n" + << "Please input the authenication code here" << std::endl ; std::string code ; std::cin >> code ; - OAuth2 token ; + OAuth2 token( client_id, client_secret ) ; token.Auth( code ) ; - // print the refresh token an exist - std::cout << "got refresh token: " << token.RefreshToken() << std::endl ; - // save to config config.Add( "refresh_token", Json( token.RefreshToken() ) ) ; assert( config["refresh_token"].As() == token.RefreshToken() ) ; @@ -98,7 +102,7 @@ int main( int argc, char **argv ) } } - OAuth2 token( config["refresh_token"].As() ) ; + OAuth2 token( config["refresh_token"].As(), client_id, client_secret ) ; Drive drive( token ) ; return 0 ; diff --git a/src/Download.cc b/src/protocol/Download.cc similarity index 95% rename from src/Download.cc rename to src/protocol/Download.cc index 3f8467d..b24be99 100644 --- a/src/Download.cc +++ b/src/protocol/Download.cc @@ -4,8 +4,8 @@ This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License - as published by the Free Software Foundation; either version 2 - of the License, or (at your option) any later version. + as published by the Free Software Foundation; version 2 + of the License. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of diff --git a/src/Download.hh b/src/protocol/Download.hh similarity index 90% rename from src/Download.hh rename to src/protocol/Download.hh index 33996c5..df31b96 100644 --- a/src/Download.hh +++ b/src/protocol/Download.hh @@ -4,8 +4,8 @@ This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License - as published by the Free Software Foundation; either version 2 - of the License, or (at your option) any later version. + as published by the Free Software Foundation; version 2 + of the License. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of diff --git a/src/HTTP.cc b/src/protocol/HTTP.cc similarity index 100% rename from src/HTTP.cc rename to src/protocol/HTTP.cc diff --git a/src/HTTP.hh b/src/protocol/HTTP.hh similarity index 93% rename from src/HTTP.hh rename to src/protocol/HTTP.hh index e08a0c9..092c0be 100644 --- a/src/HTTP.hh +++ b/src/protocol/HTTP.hh @@ -4,8 +4,8 @@ This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License - as published by the Free Software Foundation; either version 2 - of the License, or (at your option) any later version. + as published by the Free Software Foundation; version 2 + of the License. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of diff --git a/src/Json.cc b/src/protocol/Json.cc similarity index 97% rename from src/Json.cc rename to src/protocol/Json.cc index e99b3c3..26e3225 100644 --- a/src/Json.cc +++ b/src/protocol/Json.cc @@ -4,8 +4,8 @@ This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License - as published by the Free Software Foundation; either version 2 - of the License, or (at your option) any later version. + as published by the Free Software Foundation; version 2 + of the License. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of diff --git a/src/Json.hh b/src/protocol/Json.hh similarity index 93% rename from src/Json.hh rename to src/protocol/Json.hh index b80c641..63f34a3 100644 --- a/src/Json.hh +++ b/src/protocol/Json.hh @@ -4,8 +4,8 @@ This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License - as published by the Free Software Foundation; either version 2 - of the License, or (at your option) any later version. + as published by the Free Software Foundation; version 2 + of the License. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of