+ server.logger.level = 0
+ trap("INT") { server.shutdown }
+
+ server.mount("/", OAuthVerifierServlet)
+
+ oauth_client = Signet::OAuth1::Client.new(
+ :temporary_credential_uri =>
+ 'https://www.google.com/accounts/OAuthGetRequestToken',
+ :authorization_uri =>
+ 'https://www.google.com/accounts/OAuthAuthorizeToken',
+ :token_credential_uri =>
+ 'https://www.google.com/accounts/OAuthGetAccessToken',
+ :client_credential_key => 'anonymous',
+ :client_credential_secret => 'anonymous',
+ :callback => "http://localhost:#{OAUTH_SERVER_PORT}/"
+ )
+ oauth_client.fetch_temporary_credential!(:additional_parameters => {
+ :scope => options[:scope],
+ :xoauth_displayname => 'Google API Client'
+ })
+
+ # Launch browser
+ Launchy::Browser.run(oauth_client.authorization_uri.to_s)
+
+ server.start
+ oauth_client.fetch_token_credential!(:verifier => $verifier)
+ config = {
+ "scope" => options[:scope],
+ "client_credential_key" =>
+ oauth_client.client_credential_key,
+ "client_credential_secret" =>
+ oauth_client.client_credential_secret,
+ "token_credential_key" =>
+ oauth_client.token_credential_key,
+ "token_credential_secret" =>
+ oauth_client.token_credential_secret
+ }
+ config_file = File.expand_path('~/.google-api.yaml')
+ open(config_file, 'w') { |file| file.write(YAML.dump(config)) }
+ exit(0)
+ end
+ end
+
+ def oauth_2_login
+ require 'signet/oauth_2/client'
+ require 'launchy'
+ require 'yaml'
+ if !options[:client_credential_key] ||
+ !options[:client_credential_secret]
+ STDERR.puts('No client ID and secret supplied.')
+ exit(1)
+ end
+ if options[:access_token]
+ config = {
+ "mechanism" => "oauth_2",
+ "scope" => options[:scope],
+ "client_id" => options[:client_credential_key],
+ "client_secret" => options[:client_credential_secret],
+ "access_token" => options[:access_token],
+ "refresh_token" => options[:refresh_token]
+ }
+ config_file = File.expand_path('~/.google-api.yaml')
+ open(config_file, 'w') { |file| file.write(YAML.dump(config)) }
+ exit(0)
+ else
+ $verifier = nil
+ logger = WEBrick::Log.new
+ logger.level = 0
+ server = WEBrick::HTTPServer.new(
+ :Port => OAUTH_SERVER_PORT,
+ :Logger => logger,
+ :AccessLog => logger
+ )
+ trap("INT") { server.shutdown }
+
+ server.mount("/", OAuthVerifierServlet)
+
+ oauth_client = Signet::OAuth2::Client.new(
+ :authorization_uri =>
+ 'https://www.google.com/accounts/o8/oauth2/authorization',
+ :token_credential_uri =>
+ 'https://www.google.com/accounts/o8/oauth2/token',
+ :client_id => options[:client_credential_key],
+ :client_secret => options[:client_credential_secret],
+ :redirect_uri => "http://localhost:#{OAUTH_SERVER_PORT}/",
+ :scope => options[:scope]
+ )
+
+ # Launch browser
+ Launchy.open(oauth_client.authorization_uri.to_s)
+
+ server.start
+ oauth_client.code = $verifier
+ oauth_client.fetch_access_token!
+ config = {
+ "mechanism" => "oauth_2",
+ "scope" => options[:scope],
+ "client_id" => oauth_client.client_id,
+ "client_secret" => oauth_client.client_secret,
+ "access_token" => oauth_client.access_token,
+ "refresh_token" => oauth_client.refresh_token
+ }
+ config_file = File.expand_path('~/.google-api.yaml')
+ open(config_file, 'w') { |file| file.write(YAML.dump(config)) }
+ exit(0)
+ end
+ end
+
+ def list
+ api_name = options[:api]
+ unless api_name
+ STDERR.puts('No API name supplied.')