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]
31 response.status = WEBrick::HTTPStatus::RC_ACCEPTED
32 # This javascript will auto-close the tab after the
33 # verifier is obtained.
34 response.body = <<-HTML
38 function closeWindow() {
39 window.open('', '_self', '');
42 setTimeout(closeWindow, 10);
46 You may close this window.
51 server = self.instance_variable_get('@server')
56 # Initialize with default parameter values
59 :command => 'execute',
64 if @argv.first =~ /^[a-z0-9][a-z0-9_-]*$/i
65 self.options[:command] = @argv.shift
67 if @argv.first =~ /^[a-z0-9_-]+\.[a-z0-9_\.-]+$/i
68 self.options[:rpcname] = @argv.shift
76 return self.options[:command]
80 return self.options[:rpcname]
84 @parser ||= OptionParser.new do |opts|
85 opts.banner = "Usage: google-api " +
86 "(execute <rpcname> | [command]) [options] [-- <parameters>]"
88 opts.separator "\nAvailable options:"
91 "--scope <scope>", String, "Set the OAuth scope") do |s|
95 "--client-key <key>", String,
96 "Set the 2-legged OAuth key") do |k|
97 options[:client_credential_key] = k
100 "--client-secret <secret>", String,
101 "Set the 2-legged OAuth secret") do |s|
102 options[:client_credential_secret] = s
105 "-s", "--service <name>", String,
106 "Perform discovery on service") do |s|
107 options[:service_name] = s
110 "--service-version <id>", String,
111 "Select service version") do |id|
112 options[:service_version] = id
115 "--content-type <format>", String,
116 "Content-Type for request") do |f|
117 # Resolve content type shortcuts
120 f = 'application/json'
122 f = 'application/xml'
124 f = 'application/atom+xml'
126 f = 'application/rss+xml'
128 options[:content_type] = f
131 "-u", "--uri <uri>", String,
132 "Sets the URI to perform a request against") do |u|
136 "--discovery-uri <uri>", String,
137 "Sets the URI to perform discovery") do |u|
138 options[:discovery_uri] = u
141 "-m", "--method <method>", String,
142 "Sets the HTTP method to use for the request") do |m|
143 options[:http_method] = m
146 "--requestor-id <email>", String,
147 "Sets the email address of the requestor") do |e|
148 options[:requestor_id] = e
151 opts.on("-v", "--verbose", "Run verbosely") do |v|
152 options[:verbose] = v
154 opts.on("-h", "--help", "Show this message") do
158 opts.on("--version", "Show version") do
159 puts "google-api-client (#{Google::APIClient::VERSION::STRING})"
164 "\nAvailable commands:\n" +
165 " oauth-login Log a user into an API\n" +
166 " list List the methods available for a service\n" +
167 " execute Execute a method on the API\n" +
168 " irb Start an interactive client session"
174 self.parser.parse!(self.argv)
175 symbol = self.command.gsub(/-/, "_").to_sym
176 if !COMMANDS.include?(symbol)
177 STDERR.puts("Invalid command: #{self.command}")
192 require 'signet/oauth_1/client'
195 if options[:client_credential_key] &&
196 options[:client_credential_secret]
197 scope = options[:scope]
200 "client_credential_key" => options[:client_credential_key],
201 "client_credential_secret" => options[:client_credential_secret],
202 "token_credential_key" => nil,
203 "token_credential_secret" => nil
205 config_file = File.expand_path('~/.google-api.yaml')
206 open(config_file, 'w') { |file| file.write(YAML.dump(config)) }
210 # TODO(bobaman): Cross-platform?
211 logger = WEBrick::Log.new('/dev/null')
212 server = WEBrick::HTTPServer.new(
213 :Port => OAUTH_SERVER_PORT,
217 trap("INT") { server.shutdown }
219 server.mount("/", OAuthVerifierServlet)
221 oauth_client = Signet::OAuth1::Client.new(
222 :temporary_credential_uri =>
223 'https://www.google.com/accounts/OAuthGetRequestToken',
224 :authorization_uri =>
225 'https://www.google.com/accounts/OAuthAuthorizeToken',
226 :token_credential_uri =>
227 'https://www.google.com/accounts/OAuthGetAccessToken',
228 :client_credential_key => 'anonymous',
229 :client_credential_secret => 'anonymous',
230 :callback => "http://localhost:#{OAUTH_SERVER_PORT}/"
232 scope = options[:scope]
235 when /https:\/\/www\.googleapis\.com\/auth\/buzz/,
236 /https:\/\/www\.googleapis\.com\/auth\/buzz\.readonly/
237 oauth_client.authorization_uri =
238 'https://www.google.com/buzz/api/auth/OAuthAuthorizeToken?' +
239 "domain=#{oauth_client.client_credential_key}&" +
241 "xoauth_displayname=Google%20API%20Client"
243 oauth_client.fetch_temporary_credential!(:additional_parameters => {
245 :xoauth_displayname => 'Google API Client'
249 Launchy::Browser.run(oauth_client.authorization_uri.to_s)
252 oauth_client.fetch_token_credential!(:verifier => $verifier)
255 "client_credential_key" =>
256 oauth_client.client_credential_key,
257 "client_credential_secret" =>
258 oauth_client.client_credential_secret,
259 "token_credential_key" =>
260 oauth_client.token_credential_key,
261 "token_credential_secret" =>
262 oauth_client.token_credential_secret
264 config_file = File.expand_path('~/.google-api.yaml')
265 open(config_file, 'w') { |file| file.write(YAML.dump(config)) }
271 service_name = options[:service_name]
273 STDERR.puts('No service name supplied.')
276 client = Google::APIClient.new(
277 :service => service_name,
278 :authorization => nil
280 if options[:discovery_uri]
281 client.discovery_uri = options[:discovery_uri]
284 options[:service_version] ||
285 client.latest_service_version(service_name).version
286 service = client.discovered_service(service_name, service_version)
287 rpcnames = service.to_h.keys
288 puts rpcnames.sort.join("\n")
293 require 'signet/oauth_1/client'
295 config_file = File.expand_path('~/.google-api.yaml')
296 signed = File.exist?(config_file)
297 authorization_type = :oauth_1
299 # Setup HTTP request data
301 input_streams, _, _ = IO.select([STDIN], [], [], 0)
302 request_body = STDIN.read || '' if input_streams
304 if options[:content_type]
305 headers << ['Content-Type', options[:content_type]]
308 headers << ['Content-Type', 'application/json']
311 configure_authorization = lambda do |client|
312 if !client.authorization.kind_of?(Signet::OAuth1::Client)
314 "Unexpected authorization mechanism: " +
315 "#{client.authorization.class}"
319 config = open(config_file, 'r') { |file| YAML.load(file.read) }
320 client.authorization.client_credential_key =
321 config["client_credential_key"]
322 client.authorization.client_credential_secret =
323 config["client_credential_secret"]
324 client.authorization.token_credential_key =
325 config["token_credential_key"]
326 client.authorization.token_credential_secret =
327 config["token_credential_secret"]
328 if client.authorization.token_credential == nil
329 authorization_type = :two_legged_oauth_1
334 # Make request with URI manually specified
335 uri = Addressable::URI.parse(options[:uri])
337 STDERR.puts('URI may not be relative.')
340 if options[:requestor_id]
341 uri.query_values = uri.query_values.merge(
342 'xoauth_requestor_id' => options[:requestor_id]
345 method = options[:http_method]
346 method ||= request_body == '' ? 'GET' : 'POST'
348 client = Google::APIClient.new(:authorization => authorization_type)
349 if options[:discovery_uri]
350 client.discovery_uri = options[:discovery_uri]
352 configure_authorization.call(client) if signed
353 request = [method, uri.to_str, headers, [request_body]]
354 request = client.sign_request(request)
355 response = client.transmit_request(request)
356 status, headers, body = response
360 # Make request with URI generated from template and parameters
362 STDERR.puts('No rpcname supplied.')
366 options[:service_name] || self.rpcname[/^([^\.]+)\./, 1]
367 client = Google::APIClient.new(
368 :service => service_name,
369 :authorization => authorization_type
371 if options[:discovery_uri]
372 client.discovery_uri = options[:discovery_uri]
374 configure_authorization.call(client) if signed
376 options[:service_version] ||
377 client.latest_service_version(service_name).version
378 service = client.discovered_service(service_name, service_version)
379 method = service.to_h[self.rpcname]
382 "Method #{self.rpcname} does not exist for " +
383 "#{service_name}-#{service_version}."
387 parameters = self.argv.inject({}) do |accu, pair|
388 name, value = pair.split('=', 2)
392 if options[:requestor_id]
393 parameters['xoauth_requestor_id'] = options[:requestor_id]
396 response = client.execute(
397 method, parameters, request_body, headers, {:signed => signed}
399 status, headers, body = response
402 rescue ArgumentError => e
410 require 'signet/oauth_1/client'
413 config_file = File.expand_path('~/.google-api.yaml')
414 signed = File.exist?(config_file)
416 $client = Google::APIClient.new(
417 :service => options[:service_name],
418 :authorization => (signed ? :oauth_1 : nil)
422 if $client.authorization &&
423 !$client.authorization.kind_of?(Signet::OAuth1::Client)
425 "Unexpected authorization mechanism: " +
426 "#{$client.authorization.class}"
430 config = open(config_file, 'r') { |file| YAML.load(file.read) }
431 $client.authorization.client_credential_key =
432 config["client_credential_key"]
433 $client.authorization.client_credential_secret =
434 config["client_credential_secret"]
435 $client.authorization.token_credential_key =
436 config["token_credential_key"]
437 $client.authorization.token_credential_secret =
438 config["token_credential_secret"]
441 # Otherwise IRB will misinterpret command-line options
447 STDERR.puts('API fuzzing not yet supported.')
449 # Fuzz just one method
451 # Fuzz the entire API
464 Google::APIClient::CLI.new(ARGV).parse!