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
15 require 'google/api_client/version'
16 require 'google/api_client'
18 ARGV.unshift('--help') if ARGV.empty?
23 # Used for oauth login
24 class OAuthVerifierServlet < WEBrick::HTTPServlet::AbstractServlet
27 def do_GET(request, response)
28 $verifier ||= Addressable::URI.unencode_component(
29 request.request_uri.to_s[/\?.*oauth_verifier=([^&$]+)(&|$)/, 1] ||
30 request.request_uri.to_s[/\?.*code=([^&$]+)(&|$)/, 1]
32 response.status = WEBrick::HTTPStatus::RC_ACCEPTED
33 # This javascript will auto-close the tab after the
34 # verifier is obtained.
35 response.body = <<-HTML
39 function closeWindow() {
40 window.open('', '_self', '');
43 setTimeout(closeWindow, 10);
47 You may close this window.
52 server = self.instance_variable_get('@server')
57 # Initialize with default parameter values
60 :command => 'execute',
65 if @argv.first =~ /^[a-z0-9][a-z0-9_-]*$/i
66 self.options[:command] = @argv.shift
68 if @argv.first =~ /^[a-z0-9_-]+\.[a-z0-9_\.-]+$/i
69 self.options[:rpcname] = @argv.shift
77 return self.options[:command]
81 return self.options[:rpcname]
85 @parser ||= OptionParser.new do |opts|
86 opts.banner = "Usage: google-api " +
87 "(execute <rpcname> | [command]) [options] [-- <parameters>]"
89 opts.separator "\nAvailable options:"
92 "--scope <scope>", String, "Set the OAuth scope") do |s|
96 "--client-id <key>", String,
97 "Set the OAuth client id or key") do |k|
98 options[:client_credential_key] = k
101 "--client-secret <secret>", String,
102 "Set the OAuth client secret") do |s|
103 options[:client_credential_secret] = s
106 "--api <name>", String,
107 "Perform discovery on API") do |s|
111 "--api-version <id>", String,
112 "Select api version") do |id|
113 options[:version] = id
116 "--content-type <format>", String,
117 "Content-Type for request") do |f|
118 # Resolve content type shortcuts
121 f = 'application/json'
123 f = 'application/xml'
125 f = 'application/atom+xml'
127 f = 'application/rss+xml'
129 options[:content_type] = f
132 "-u", "--uri <uri>", String,
133 "Sets the URI to perform a request against") do |u|
137 "--discovery-uri <uri>", String,
138 "Sets the URI to perform discovery") do |u|
139 options[:discovery_uri] = u
142 "-m", "--method <method>", String,
143 "Sets the HTTP method to use for the request") do |m|
144 options[:http_method] = m
147 "--requestor-id <email>", String,
148 "Sets the email address of the requestor") do |e|
149 options[:requestor_id] = e
152 opts.on("-v", "--verbose", "Run verbosely") do |v|
153 options[:verbose] = v
155 opts.on("-h", "--help", "Show this message") do
159 opts.on("--version", "Show version") do
160 puts "google-api-client (#{Google::APIClient::VERSION::STRING})"
165 "\nAvailable commands:\n" +
166 " oauth-1-login Log a user into an API with OAuth 1.0a\n" +
167 " oauth-2-login Log a user into an API with OAuth 2.0 d10\n" +
168 " list List the methods available for an API\n" +
169 " execute Execute a method on the API\n" +
170 " irb Start an interactive client session"
176 self.parser.parse!(self.argv)
177 symbol = self.command.gsub(/-/, "_").to_sym
178 if !COMMANDS.include?(symbol)
179 STDERR.puts("Invalid command: #{self.command}")
186 require 'signet/oauth_1/client'
189 config_file = File.expand_path('~/.google-api.yaml')
191 if File.exist?(config_file)
192 config = open(config_file, 'r') { |file| YAML.load(file.read) }
196 if config["mechanism"]
197 authorization = config["mechanism"].to_sym
200 client = Google::APIClient.new(:authorization => authorization)
204 if client.authorization &&
205 !client.authorization.kind_of?(Signet::OAuth1::Client)
207 "Unexpected authorization mechanism: " +
208 "#{client.authorization.class}"
212 config = open(config_file, 'r') { |file| YAML.load(file.read) }
213 client.authorization.client_credential_key =
214 config["client_credential_key"]
215 client.authorization.client_credential_secret =
216 config["client_credential_secret"]
217 client.authorization.token_credential_key =
218 config["token_credential_key"]
219 client.authorization.token_credential_secret =
220 config["token_credential_secret"]
222 if client.authorization &&
223 !client.authorization.kind_of?(Signet::OAuth2::Client)
225 "Unexpected authorization mechanism: " +
226 "#{client.authorization.class}"
230 config = open(config_file, 'r') { |file| YAML.load(file.read) }
231 client.authorization.scope = options[:scope]
232 client.authorization.client_id = config["client_id"]
233 client.authorization.client_secret = config["client_secret"]
234 client.authorization.access_token = config["access_token"]
235 client.authorization.refresh_token = config["refresh_token"]
240 if options[:discovery_uri]
241 if options[:api] && options[:version]
242 client.register_discovery_uri(
243 options[:api], options[:version], options[:discovery_uri]
247 'Cannot register a discovery URI without ' +
248 'specifying an API and version.'
257 def api_version(api_name, version)
260 if client.preferred_version(api_name)
261 v = client.preferred_version(api_name).version
279 require 'signet/oauth_1/client'
282 if options[:client_credential_key] &&
283 options[:client_credential_secret]
285 "mechanism" => "oauth_1",
286 "scope" => options[:scope],
287 "client_credential_key" => options[:client_credential_key],
288 "client_credential_secret" => options[:client_credential_secret],
289 "token_credential_key" => nil,
290 "token_credential_secret" => nil
292 config_file = File.expand_path('~/.google-api.yaml')
293 open(config_file, 'w') { |file| file.write(YAML.dump(config)) }
297 server = WEBrick::HTTPServer.new(
298 :Port => OAUTH_SERVER_PORT,
299 :Logger => WEBrick::Log.new,
300 :AccessLog => WEBrick::Log.new
302 server.logger.level = 0
303 trap("INT") { server.shutdown }
305 server.mount("/", OAuthVerifierServlet)
307 oauth_client = Signet::OAuth1::Client.new(
308 :temporary_credential_uri =>
309 'https://www.google.com/accounts/OAuthGetRequestToken',
310 :authorization_uri =>
311 'https://www.google.com/accounts/OAuthAuthorizeToken',
312 :token_credential_uri =>
313 'https://www.google.com/accounts/OAuthGetAccessToken',
314 :client_credential_key => 'anonymous',
315 :client_credential_secret => 'anonymous',
316 :callback => "http://localhost:#{OAUTH_SERVER_PORT}/"
318 oauth_client.fetch_temporary_credential!(:additional_parameters => {
319 :scope => options[:scope],
320 :xoauth_displayname => 'Google API Client'
324 Launchy::Browser.run(oauth_client.authorization_uri.to_s)
327 oauth_client.fetch_token_credential!(:verifier => $verifier)
329 "scope" => options[:scope],
330 "client_credential_key" =>
331 oauth_client.client_credential_key,
332 "client_credential_secret" =>
333 oauth_client.client_credential_secret,
334 "token_credential_key" =>
335 oauth_client.token_credential_key,
336 "token_credential_secret" =>
337 oauth_client.token_credential_secret
339 config_file = File.expand_path('~/.google-api.yaml')
340 open(config_file, 'w') { |file| file.write(YAML.dump(config)) }
346 require 'signet/oauth_2/client'
349 if !options[:client_credential_key] ||
350 !options[:client_credential_secret]
351 STDERR.puts('No client ID and secret supplied.')
354 if options[:access_token]
356 "mechanism" => "oauth_2",
357 "scope" => options[:scope],
358 "client_id" => options[:client_credential_key],
359 "client_secret" => options[:client_credential_secret],
360 "access_token" => options[:access_token],
361 "refresh_token" => options[:refresh_token]
363 config_file = File.expand_path('~/.google-api.yaml')
364 open(config_file, 'w') { |file| file.write(YAML.dump(config)) }
368 # TODO(bobaman): Cross-platform?
369 logger = WEBrick::Log.new('/dev/null')
370 server = WEBrick::HTTPServer.new(
371 :Port => OAUTH_SERVER_PORT,
375 trap("INT") { server.shutdown }
377 server.mount("/", OAuthVerifierServlet)
379 oauth_client = Signet::OAuth2::Client.new(
380 :authorization_uri =>
381 'https://www.google.com/accounts/o8/oauth2/authorization',
382 :token_credential_uri =>
383 'https://www.google.com/accounts/o8/oauth2/token',
384 :client_id => options[:client_credential_key],
385 :client_secret => options[:client_credential_secret],
386 :redirect_uri => "http://localhost:#{OAUTH_SERVER_PORT}/",
387 :scope => options[:scope]
391 Launchy.open(oauth_client.authorization_uri.to_s)
394 oauth_client.code = $verifier
395 oauth_client.fetch_access_token!
397 "mechanism" => "oauth_2",
398 "scope" => options[:scope],
399 "client_id" => oauth_client.client_id,
400 "client_secret" => oauth_client.client_secret,
401 "access_token" => oauth_client.access_token,
402 "refresh_token" => oauth_client.refresh_token
404 config_file = File.expand_path('~/.google-api.yaml')
405 open(config_file, 'w') { |file| file.write(YAML.dump(config)) }
411 api_name = options[:api]
413 STDERR.puts('No API name supplied.')
416 client = Google::APIClient.new(:authorization => nil)
417 if options[:discovery_uri]
418 if options[:api] && options[:version]
419 client.register_discovery_uri(
420 options[:api], options[:version], options[:discovery_uri]
424 'Cannot register a discovery URI without ' +
425 'specifying an API and version.'
430 version = api_version(api_name, options[:version])
431 api = client.discovered_api(api_name, version)
432 rpcnames = api.to_h.keys
433 puts rpcnames.sort.join("\n")
440 # Setup HTTP request data
442 input_streams, _, _ = IO.select([STDIN], [], [], 0)
443 request_body = STDIN.read || '' if input_streams
445 if options[:content_type]
446 headers << ['Content-Type', options[:content_type]]
449 headers << ['Content-Type', 'application/json']
453 # Make request with URI manually specified
454 uri = Addressable::URI.parse(options[:uri])
456 STDERR.puts('URI may not be relative.')
459 if options[:requestor_id]
460 uri.query_values = uri.query_values.merge(
461 'xoauth_requestor_id' => options[:requestor_id]
464 method = options[:http_method]
465 method ||= request_body == '' ? 'GET' : 'POST'
467 request = [method, uri.to_str, headers, [request_body]]
468 request = client.generate_authenticated_request(:request => request)
469 response = client.transmit(request)
470 status, headers, body = response
474 # Make request with URI generated from template and parameters
476 STDERR.puts('No rpcname supplied.')
479 api_name = options[:api] || self.rpcname[/^([^\.]+)\./, 1]
480 version = api_version(api_name, options[:version])
481 api = client.discovered_api(api_name, version)
482 method = api.to_h[self.rpcname]
485 "Method #{self.rpcname} does not exist for " +
486 "#{api_name}-#{version}."
490 parameters = self.argv.inject({}) do |accu, pair|
491 name, value = pair.split('=', 2)
495 if options[:requestor_id]
496 parameters['xoauth_requestor_id'] = options[:requestor_id]
499 result = client.execute(
500 :api_method => method,
501 :parameters => parameters,
502 :merged_body => request_body,
505 status, headers, body = result.response
508 rescue ArgumentError => e
516 $client = self.client
517 # Otherwise IRB will misinterpret command-line options
523 STDERR.puts('API fuzzing not yet supported.')
525 # Fuzz just one method
527 # Fuzz the entire API
540 Google::APIClient::CLI.new(ARGV).parse!