diff --git a/grive/src/main.cc b/grive/src/main.cc index 9e89285..d791190 100644 --- a/grive/src/main.cc +++ b/grive/src/main.cc @@ -249,11 +249,11 @@ int Main( int argc, char **argv ) std::string secret = vm.count( "secret" ) > 0 ? vm["secret"].as() : default_secret ; - std::string uri = vm.count( "redirect-uri" ) > 0 + std::string redirect_uri = vm.count( "redirect-uri" ) > 0 ? vm["redirect-uri"].as() : default_redirect_uri ; - OAuth2 token( http.get(), id, secret ) ; + OAuth2 token( http.get(), id, secret, redirect_uri ) ; if ( vm.count("print-url") ) { @@ -267,24 +267,27 @@ int Main( int argc, char **argv ) << token.MakeAuthURL() << std::endl ; - std::string code = AuthCode(uri); + std::string code = AuthCode(redirect_uri); token.Auth( code ) ; // save to config config.Set( "id", Val( id ) ) ; config.Set( "secret", Val( secret ) ) ; config.Set( "refresh_token", Val( token.RefreshToken() ) ) ; + config.Set( "redirect_uri", Val( redirect_uri ) ) ; config.Save() ; } std::string refresh_token ; std::string id ; std::string secret ; + std::string redirect_uri ; try { refresh_token = config.Get("refresh_token").Str() ; id = config.Get("id").Str() ; secret = config.Get("secret").Str() ; + redirect_uri = config.Get("redirect_uri").Str() ; } catch ( Exception& e ) { @@ -296,7 +299,7 @@ int Main( int argc, char **argv ) return -1; } - OAuth2 token( http.get(), refresh_token, id, secret ) ; + OAuth2 token( http.get(), refresh_token, id, secret, redirect_uri ) ; AuthAgent agent( token, http.get() ) ; v2::Syncer2 syncer( &agent ); diff --git a/libgrive/src/protocol/OAuth2.cc b/libgrive/src/protocol/OAuth2.cc index db1858a..0562c1f 100644 --- a/libgrive/src/protocol/OAuth2.cc +++ b/libgrive/src/protocol/OAuth2.cc @@ -35,12 +35,14 @@ const std::string token_url = "https://accounts.google.com/o/oauth2/token" ; OAuth2::OAuth2( http::Agent* agent, const std::string& refresh_code, - const std::string& client_id, - const std::string& client_secret ) : + const std::string& client_id, + const std::string& client_secret, + const std::string& redirect_uri ) : m_refresh( refresh_code ), m_agent( agent ), m_client_id( client_id ), - m_client_secret( client_secret ) + m_client_secret( client_secret ), + m_redirect_uri ( redirect_uri ) { Refresh( ) ; } @@ -48,10 +50,12 @@ OAuth2::OAuth2( OAuth2::OAuth2( http::Agent* agent, const std::string& client_id, - const std::string& client_secret ) : + const std::string& client_secret, + const std::string& redirect_uri ) : m_agent( agent ), m_client_id( client_id ), - m_client_secret( client_secret ) + m_client_secret( client_secret ), + m_redirect_uri( redirect_uri ) { } @@ -61,7 +65,7 @@ void OAuth2::Auth( const std::string& auth_code ) "code=" + auth_code + "&client_id=" + m_client_id + "&client_secret=" + m_client_secret + - "&redirect_uri=" + "urn:ietf:wg:oauth:2.0:oob" + + "&redirect_uri=" + m_redirect_uri + "&grant_type=authorization_code" ; http::ValResponse resp ; @@ -85,7 +89,7 @@ std::string OAuth2::MakeAuthURL() { return "https://accounts.google.com/o/oauth2/auth" "?scope=" + m_agent->Escape( "https://www.googleapis.com/auth/drive" ) + - "&redirect_uri=urn:ietf:wg:oauth:2.0:oob" + "&redirect_uri=" + m_redirect_uri + "&response_type=code" "&client_id=" + m_client_id ; } diff --git a/libgrive/src/protocol/OAuth2.hh b/libgrive/src/protocol/OAuth2.hh index e9a23da..4598f09 100644 --- a/libgrive/src/protocol/OAuth2.hh +++ b/libgrive/src/protocol/OAuth2.hh @@ -35,12 +35,14 @@ public : OAuth2( http::Agent* agent, const std::string& client_id, - const std::string& client_secret ) ; + const std::string& client_secret, + const std::string& redirect_uri ) ; OAuth2( http::Agent* agent, const std::string& refresh_code, const std::string& client_id, - const std::string& client_secret ) ; + const std::string& client_secret, + const std::string& redirect_uri ) ; std::string Str() const ; @@ -62,6 +64,7 @@ private : const std::string m_client_id ; const std::string m_client_secret ; + const std::string m_redirect_uri ; } ; } // end of namespace diff --git a/libgrive/src/util/Config.cc b/libgrive/src/util/Config.cc index c08972f..261de6e 100644 --- a/libgrive/src/util/Config.cc +++ b/libgrive/src/util/Config.cc @@ -47,6 +47,8 @@ Config::Config( const po::variables_map& vm ) m_cmd.Add( "path", Val(vm.count("path") > 0 ? vm["path"].as() : default_root_folder ) ) ; + if ( vm.count( "redirect_uri" ) > 0 ) + m_cmd.Add( "redirect_uri", Val( vm["redirect_uri"].as() ) ) ; m_cmd.Add( "dir", Val(vm.count("dir") > 0 ? vm["dir"].as() : "" ) ) ;