3 bin_dir = File.expand_path("..", __FILE__)
4 lib_dir = File.expand_path("../lib", bin_dir)
6 $LOAD_PATH.unshift(lib_dir)
9 OAUTH_SERVER_PORT = 12736
14 require 'faraday/utils'
16 require 'google/api_client/version'
17 require 'google/api_client'
19 ARGV.unshift('--help') if ARGV.empty?
24 # Used for oauth login
25 class OAuthVerifierServlet < WEBrick::HTTPServlet::AbstractServlet
28 def do_GET(request, response)
29 $verifier ||= Addressable::URI.unencode_component(
30 request.request_uri.to_s[/\?.*oauth_verifier=([^&$]+)(&|$)/, 1] ||
31 request.request_uri.to_s[/\?.*code=([^&$]+)(&|$)/, 1]
33 response.status = WEBrick::HTTPStatus::RC_ACCEPTED
34 # This javascript will auto-close the tab after the
35 # verifier is obtained.
36 response.body = <<-HTML
40 function closeWindow() {
41 window.open('', '_self', '');
44 setTimeout(closeWindow, 10);
48 You may close this window.
53 server = self.instance_variable_get('@server')
58 # Initialize with default parameter values
61 :command => 'execute',
66 if @argv.first =~ /^[a-z0-9][a-z0-9_-]*$/i
67 self.options[:command] = @argv.shift
69 if @argv.first =~ /^[a-z0-9_-]+\.[a-z0-9_\.-]+$/i
70 self.options[:rpcname] = @argv.shift
78 return self.options[:command]
82 return self.options[:rpcname]
86 @parser ||= OptionParser.new do |opts|
87 opts.banner = "Usage: google-api " +
88 "(execute <rpcname> | [command]) [options] [-- <parameters>]"
90 opts.separator "\nAvailable options:"
93 "--scope <scope>", String, "Set the OAuth scope") do |s|
97 "--client-id <key>", String,
98 "Set the OAuth client id or key") do |k|
99 options[:client_credential_key] = k
102 "--client-secret <secret>", String,
103 "Set the OAuth client secret") do |s|
104 options[:client_credential_secret] = s
107 "--api <name>", String,
108 "Perform discovery on API") do |s|
112 "--api-version <id>", String,
113 "Select api version") do |id|
114 options[:version] = id
117 "--content-type <format>", String,
118 "Content-Type for request") do |f|
119 # Resolve content type shortcuts
122 f = 'application/json'
124 f = 'application/xml'
126 f = 'application/atom+xml'
128 f = 'application/rss+xml'
130 options[:content_type] = f
133 "-u", "--uri <uri>", String,
134 "Sets the URI to perform a request against") do |u|
138 "--discovery-uri <uri>", String,
139 "Sets the URI to perform discovery") do |u|
140 options[:discovery_uri] = u
143 "-m", "--method <method>", String,
144 "Sets the HTTP method to use for the request") do |m|
145 options[:http_method] = m
148 "--requestor-id <email>", String,
149 "Sets the email address of the requestor") do |e|
150 options[:requestor_id] = e
153 opts.on("-v", "--verbose", "Run verbosely") do |v|
154 options[:verbose] = v
156 opts.on("-h", "--help", "Show this message") do
160 opts.on("--version", "Show version") do
161 puts "google-api-client (#{Google::APIClient::VERSION::STRING})"
166 "\nAvailable commands:\n" +
167 " oauth-1-login Log a user into an API with OAuth 1.0a\n" +
168 " oauth-2-login Log a user into an API with OAuth 2.0 d10\n" +
169 " list List the methods available for an API\n" +
170 " execute Execute a method on the API\n" +
171 " irb Start an interactive client session"
177 self.parser.parse!(self.argv)
178 symbol = self.command.gsub(/-/, "_").to_sym
179 if !COMMANDS.include?(symbol)
180 STDERR.puts("Invalid command: #{self.command}")
187 require 'signet/oauth_1/client'
190 config_file = File.expand_path('~/.google-api.yaml')
192 if File.exist?(config_file)
193 config = open(config_file, 'r') { |file| YAML.load(file.read) }
197 if config["mechanism"]
198 authorization = config["mechanism"].to_sym
201 client = Google::APIClient.new(:authorization => authorization)
205 if client.authorization &&
206 !client.authorization.kind_of?(Signet::OAuth1::Client)
208 "Unexpected authorization mechanism: " +
209 "#{client.authorization.class}"
213 config = open(config_file, 'r') { |file| YAML.load(file.read) }
214 client.authorization.client_credential_key =
215 config["client_credential_key"]
216 client.authorization.client_credential_secret =
217 config["client_credential_secret"]
218 client.authorization.token_credential_key =
219 config["token_credential_key"]
220 client.authorization.token_credential_secret =
221 config["token_credential_secret"]
223 if client.authorization &&
224 !client.authorization.kind_of?(Signet::OAuth2::Client)
226 "Unexpected authorization mechanism: " +
227 "#{client.authorization.class}"
231 config = open(config_file, 'r') { |file| YAML.load(file.read) }
232 client.authorization.scope = options[:scope]
233 client.authorization.client_id = config["client_id"]
234 client.authorization.client_secret = config["client_secret"]
235 client.authorization.access_token = config["access_token"]
236 client.authorization.refresh_token = config["refresh_token"]
241 if options[:discovery_uri]
242 if options[:api] && options[:version]
243 client.register_discovery_uri(
244 options[:api], options[:version], options[:discovery_uri]
248 'Cannot register a discovery URI without ' +
249 'specifying an API and version.'
258 def api_version(api_name, version)
261 if client.preferred_version(api_name)
262 v = client.preferred_version(api_name).version
280 require 'signet/oauth_1/client'
283 if options[:client_credential_key] &&
284 options[:client_credential_secret]
286 "mechanism" => "oauth_1",
287 "scope" => options[:scope],
288 "client_credential_key" => options[:client_credential_key],
289 "client_credential_secret" => options[:client_credential_secret],
290 "token_credential_key" => nil,
291 "token_credential_secret" => nil
293 config_file = File.expand_path('~/.google-api.yaml')
294 open(config_file, 'w') { |file| file.write(YAML.dump(config)) }
298 server = WEBrick::HTTPServer.new(
299 :Port => OAUTH_SERVER_PORT,
300 :Logger => WEBrick::Log.new,
301 :AccessLog => WEBrick::Log.new
303 server.logger.level = 0
304 trap("INT") { server.shutdown }
306 server.mount("/", OAuthVerifierServlet)
308 oauth_client = Signet::OAuth1::Client.new(
309 :temporary_credential_uri =>
310 'https://www.google.com/accounts/OAuthGetRequestToken',
311 :authorization_uri =>
312 'https://www.google.com/accounts/OAuthAuthorizeToken',
313 :token_credential_uri =>
314 'https://www.google.com/accounts/OAuthGetAccessToken',
315 :client_credential_key => 'anonymous',
316 :client_credential_secret => 'anonymous',
317 :callback => "http://localhost:#{OAUTH_SERVER_PORT}/"
319 oauth_client.fetch_temporary_credential!(:additional_parameters => {
320 :scope => options[:scope],
321 :xoauth_displayname => 'Google API Client'
325 Launchy::Browser.run(oauth_client.authorization_uri.to_s)
328 oauth_client.fetch_token_credential!(:verifier => $verifier)
330 "scope" => options[:scope],
331 "client_credential_key" =>
332 oauth_client.client_credential_key,
333 "client_credential_secret" =>
334 oauth_client.client_credential_secret,
335 "token_credential_key" =>
336 oauth_client.token_credential_key,
337 "token_credential_secret" =>
338 oauth_client.token_credential_secret
340 config_file = File.expand_path('~/.google-api.yaml')
341 open(config_file, 'w') { |file| file.write(YAML.dump(config)) }
347 require 'signet/oauth_2/client'
350 if !options[:client_credential_key] ||
351 !options[:client_credential_secret]
352 STDERR.puts('No client ID and secret supplied.')
355 if options[:access_token]
357 "mechanism" => "oauth_2",
358 "scope" => options[:scope],
359 "client_id" => options[:client_credential_key],
360 "client_secret" => options[:client_credential_secret],
361 "access_token" => options[:access_token],
362 "refresh_token" => options[:refresh_token]
364 config_file = File.expand_path('~/.google-api.yaml')
365 open(config_file, 'w') { |file| file.write(YAML.dump(config)) }
369 logger = WEBrick::Log.new
371 server = WEBrick::HTTPServer.new(
372 :Port => OAUTH_SERVER_PORT,
376 trap("INT") { server.shutdown }
378 server.mount("/", OAuthVerifierServlet)
380 oauth_client = Signet::OAuth2::Client.new(
381 :authorization_uri =>
382 'https://www.google.com/accounts/o8/oauth2/authorization',
383 :token_credential_uri =>
384 'https://www.google.com/accounts/o8/oauth2/token',
385 :client_id => options[:client_credential_key],
386 :client_secret => options[:client_credential_secret],
387 :redirect_uri => "http://localhost:#{OAUTH_SERVER_PORT}/",
388 :scope => options[:scope]
392 Launchy.open(oauth_client.authorization_uri.to_s)
395 oauth_client.code = $verifier
396 oauth_client.fetch_access_token!
398 "mechanism" => "oauth_2",
399 "scope" => options[:scope],
400 "client_id" => oauth_client.client_id,
401 "client_secret" => oauth_client.client_secret,
402 "access_token" => oauth_client.access_token,
403 "refresh_token" => oauth_client.refresh_token
405 config_file = File.expand_path('~/.google-api.yaml')
406 open(config_file, 'w') { |file| file.write(YAML.dump(config)) }
412 api_name = options[:api]
414 STDERR.puts('No API name supplied.')
417 client = Google::APIClient.new(:authorization => nil)
418 if options[:discovery_uri]
419 if options[:api] && options[:version]
420 client.register_discovery_uri(
421 options[:api], options[:version], options[:discovery_uri]
425 'Cannot register a discovery URI without ' +
426 'specifying an API and version.'
431 version = api_version(api_name, options[:version])
432 api = client.discovered_api(api_name, version)
433 rpcnames = api.to_h.keys
434 puts rpcnames.sort.join("\n")
441 # Setup HTTP request data
443 input_streams, _, _ = IO.select([STDIN], [], [], 0)
444 request_body = STDIN.read || '' if input_streams
446 if options[:content_type]
447 headers << ['Content-Type', options[:content_type]]
450 headers << ['Content-Type', 'application/json']
454 # Make request with URI manually specified
455 uri = Addressable::URI.parse(options[:uri])
457 STDERR.puts('URI may not be relative.')
460 if options[:requestor_id]
461 uri.query_values = uri.query_values.merge(
462 'xoauth_requestor_id' => options[:requestor_id]
465 method = options[:http_method]
466 method ||= request_body == '' ? 'GET' : 'POST'
468 response = client.execute(:http_method => method, :uri => uri.to_str,
469 :headers => headers, :body => request_body)
473 # Make request with URI generated from template and parameters
475 STDERR.puts('No rpcname supplied.')
478 api_name = options[:api] || self.rpcname[/^([^\.]+)\./, 1]
479 version = api_version(api_name, options[:version])
480 api = client.discovered_api(api_name, version)
481 method = api.to_h[self.rpcname]
484 "Method #{self.rpcname} does not exist for " +
485 "#{api_name}-#{version}."
489 parameters = self.argv.inject({}) do |accu, pair|
490 name, value = pair.split('=', 2)
494 if options[:requestor_id]
495 parameters['xoauth_requestor_id'] = options[:requestor_id]
498 result = client.execute(
499 :api_method => method,
500 :parameters => parameters,
501 :merged_body => request_body,
504 puts result.response.body
506 rescue ArgumentError => e
514 $client = self.client
515 # Otherwise IRB will misinterpret command-line options
521 STDERR.puts('API fuzzing not yet supported.')
523 # Fuzz just one method
525 # Fuzz the entire API
538 Google::APIClient::CLI.new(ARGV).parse!