2012-04-25 20:13:17 +04:00
|
|
|
/*
|
|
|
|
grive: an GPL program to sync a local directory with Google Drive
|
|
|
|
Copyright (C) 2012 Wan Wai Ho
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License
|
2012-04-26 19:40:38 +04:00
|
|
|
as published by the Free Software Foundation version 2
|
2012-04-26 08:21:40 +04:00
|
|
|
of the License.
|
2012-04-25 20:13:17 +04:00
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
2012-05-15 19:07:03 +04:00
|
|
|
#include "Config.hh"
|
|
|
|
|
2012-04-27 06:49:33 +04:00
|
|
|
#include "drive/Drive.hh"
|
2012-05-13 11:27:58 +04:00
|
|
|
|
2012-04-27 06:49:33 +04:00
|
|
|
#include "protocol/OAuth2.hh"
|
2012-04-26 08:21:40 +04:00
|
|
|
#include "protocol/Json.hh"
|
2012-04-25 20:13:17 +04:00
|
|
|
|
2012-05-13 11:27:58 +04:00
|
|
|
#include "bfd/Backtrace.hh"
|
|
|
|
#include "util/Exception.hh"
|
2012-05-13 12:10:18 +04:00
|
|
|
#include "util/Log.hh"
|
2012-05-13 13:27:40 +04:00
|
|
|
#include "util/DefaultLog.hh"
|
2012-05-13 11:27:58 +04:00
|
|
|
|
|
|
|
#include <boost/exception/all.hpp>
|
|
|
|
|
2012-04-26 05:20:41 +04:00
|
|
|
#include <cassert>
|
2012-04-25 20:13:17 +04:00
|
|
|
#include <cstdlib>
|
|
|
|
#include <iostream>
|
2012-04-29 19:40:13 +04:00
|
|
|
#include <unistd.h>
|
2012-04-29 02:45:33 +04:00
|
|
|
|
2012-04-26 08:21:40 +04:00
|
|
|
const std::string client_id = "22314510474.apps.googleusercontent.com" ;
|
|
|
|
const std::string client_secret = "bl4ufi89h-9MkFlypcI7R785" ;
|
|
|
|
|
2012-04-25 20:13:17 +04:00
|
|
|
int main( int argc, char **argv )
|
|
|
|
{
|
|
|
|
using namespace gr ;
|
2012-04-26 05:20:41 +04:00
|
|
|
|
2012-05-15 19:07:03 +04:00
|
|
|
Config config ;
|
2012-04-25 20:13:17 +04:00
|
|
|
|
2012-05-13 13:27:40 +04:00
|
|
|
DefaultLog nofile_log ;
|
|
|
|
LogBase::Inst( &nofile_log ) ;
|
|
|
|
|
2012-04-25 20:13:17 +04:00
|
|
|
int c ;
|
2012-05-13 13:27:40 +04:00
|
|
|
while ((c = getopt(argc, argv, "al:v")) != -1)
|
2012-04-25 20:13:17 +04:00
|
|
|
{
|
|
|
|
switch ( c )
|
|
|
|
{
|
|
|
|
case 'a' :
|
|
|
|
{
|
2012-04-26 05:20:41 +04:00
|
|
|
std::cout
|
2012-04-26 08:21:40 +04:00
|
|
|
<< "-----------------------\n"
|
2012-05-11 22:20:32 +04:00
|
|
|
<< "Please go to this URL and get an authentication code:\n\n"
|
2012-04-26 08:21:40 +04:00
|
|
|
<< OAuth2::MakeAuthURL( client_id )
|
2012-04-26 05:20:41 +04:00
|
|
|
<< std::endl ;
|
|
|
|
|
2012-04-26 08:21:40 +04:00
|
|
|
std::cout
|
|
|
|
<< "\n-----------------------\n"
|
2012-05-11 22:20:32 +04:00
|
|
|
<< "Please input the authentication code here: " << std::endl ;
|
2012-04-26 05:20:41 +04:00
|
|
|
std::string code ;
|
|
|
|
std::cin >> code ;
|
|
|
|
|
2012-04-26 08:21:40 +04:00
|
|
|
OAuth2 token( client_id, client_secret ) ;
|
2012-04-26 05:20:41 +04:00
|
|
|
token.Auth( code ) ;
|
2012-04-25 20:13:17 +04:00
|
|
|
|
2012-04-26 05:20:41 +04:00
|
|
|
// save to config
|
2012-05-15 19:07:03 +04:00
|
|
|
config.Get().Add( "refresh_token", Json( token.RefreshToken() ) ) ;
|
|
|
|
assert( config.Get()["refresh_token"].Str() == token.RefreshToken() ) ;
|
|
|
|
config.Save( ) ;
|
2012-04-26 05:20:41 +04:00
|
|
|
|
|
|
|
break ;
|
2012-04-25 20:13:17 +04:00
|
|
|
}
|
2012-05-06 18:40:58 +04:00
|
|
|
|
2012-05-13 13:27:40 +04:00
|
|
|
case 'l' :
|
|
|
|
{
|
|
|
|
static DefaultLog log( optarg ) ;
|
|
|
|
LogBase::Inst( &log ) ;
|
|
|
|
break ;
|
|
|
|
}
|
|
|
|
|
2012-05-06 18:40:58 +04:00
|
|
|
case 'v' :
|
|
|
|
{
|
|
|
|
std::cout
|
|
|
|
<< "grive version " VERSION " " __DATE__ " " __TIME__ << std::endl ;
|
|
|
|
return 0 ;
|
|
|
|
}
|
2012-04-25 20:13:17 +04:00
|
|
|
}
|
|
|
|
}
|
2012-05-13 13:27:40 +04:00
|
|
|
|
2012-04-25 20:13:17 +04:00
|
|
|
|
2012-04-29 07:17:15 +04:00
|
|
|
std::string refresh_token ;
|
2012-04-29 02:45:33 +04:00
|
|
|
try
|
|
|
|
{
|
2012-05-15 19:07:03 +04:00
|
|
|
refresh_token = config.Get()["refresh_token"].Str() ;
|
2012-04-29 02:45:33 +04:00
|
|
|
}
|
|
|
|
catch ( const std::runtime_error& error )
|
|
|
|
{
|
2012-05-13 12:45:27 +04:00
|
|
|
Log(
|
2012-05-13 12:10:18 +04:00
|
|
|
"Please run grive with the \"-a\" option if this is the "
|
|
|
|
"first time you're accessing your Google Drive!",
|
2012-05-13 12:45:27 +04:00
|
|
|
log::critical ) ;
|
2012-05-13 12:10:18 +04:00
|
|
|
|
2012-04-29 02:45:33 +04:00
|
|
|
return -1;
|
|
|
|
}
|
2012-04-25 20:13:17 +04:00
|
|
|
|
2012-05-13 11:27:58 +04:00
|
|
|
try
|
|
|
|
{
|
|
|
|
OAuth2 token( refresh_token, client_id, client_secret ) ;
|
|
|
|
Drive drive( token ) ;
|
2012-05-13 21:07:23 +04:00
|
|
|
|
|
|
|
drive.Update() ;
|
2012-05-13 11:27:58 +04:00
|
|
|
}
|
|
|
|
catch ( gr::Exception& e )
|
|
|
|
{
|
2012-05-13 12:45:27 +04:00
|
|
|
Log( "exception: %1%", boost::diagnostic_information(e), log::critical ) ;
|
2012-05-13 12:10:18 +04:00
|
|
|
return -1 ;
|
2012-05-13 11:27:58 +04:00
|
|
|
}
|
2012-04-29 07:17:15 +04:00
|
|
|
|
2012-04-25 20:13:17 +04:00
|
|
|
return 0 ;
|
|
|
|
}
|