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
13 require 'google/api_client/version'
14 require 'google/api_client'
16 ARGV.unshift('--help') if ARGV.empty?
20 OptionParser.new do |opts|
22 "Usage: google-api <rpcname> [options] -- <parameters>\n" +
23 " or: google-api --oauth-login=<scope> [options]\n" +
24 " or: google-api --fuzz [options]"
29 "--oauth-login <scope>", String, "Authorize for the scope") do |s|
30 command = 'oauth-login'
34 "-s", "--service <name>", String, "Perform discovery on service") do |s|
35 options[:service_name] = s
38 "--service-version <id>", String, "Select service version") do |id|
39 options[:service_version] = id
42 "--content-type <format>", String, "Content-Type for request") do |f|
43 # Resolve content type shortcuts
46 f = 'application/json'
50 f = 'application/atom+xml'
52 f = 'application/rss+xml'
54 options[:content_type] = f
56 opts.on("--fuzz [rpcname]", String, "Fuzz an API or endpoint") do |rpcname|
58 options[:fuzz] = rpcname
61 opts.on_tail("-v", "--verbose", "Run verbosely") do |v|
64 opts.on_tail("-h", "--help", "Show this message") do
68 opts.on_tail("--version", "Show version") do
69 puts "google-api-client (#{Google::APIClient::VERSION::STRING})"
74 if command == 'oauth-login' # Guard to keep start-up time short
76 # Used for oauth login
77 class OAuthVerifierServlet < WEBrick::HTTPServlet::AbstractServlet
78 def do_GET(request, response)
79 $verifier ||= Addressable::URI.unencode_component(
80 request.request_uri.to_s[/\?.*oauth_verifier=([^&$]+)(&|$)/, 1]
82 response.status = WEBrick::HTTPStatus::RC_ACCEPTED
83 # This javascript will auto-close the tab after the verifier is obtained.
84 response.body = <<-HTML
88 function closeWindow() {
89 window.open('', '_self', '');
92 setTimeout(closeWindow, 10);
96 You may close this window.
100 self.instance_variable_get('@server').stop
105 def oauth_login(options={})
106 require 'signet/oauth_1/client'
110 logger = WEBrick::Log.new('/dev/null') # TODO(bobaman): Cross-platform?
111 server = WEBrick::HTTPServer.new(
112 :Port => OAUTH_SERVER_PORT,
116 trap("INT") { server.shutdown }
118 server.mount("/", OAuthVerifierServlet)
120 oauth_client = Signet::OAuth1::Client.new(
121 :temporary_credential_uri =>
122 'https://www.google.com/accounts/OAuthGetRequestToken',
123 :authorization_uri =>
124 'https://www.google.com/accounts/OAuthAuthorizeToken',
125 :token_credential_uri =>
126 'https://www.google.com/accounts/OAuthGetAccessToken',
127 :client_credential_key => 'anonymous',
128 :client_credential_secret => 'anonymous',
129 :callback => "http://localhost:#{OAUTH_SERVER_PORT}/"
131 scope = options[:scope]
134 when "https://www.googleapis.com/auth/buzz",
135 "https://www.googleapis.com/auth/buzz.readonly"
136 oauth_client.authorization_uri =
137 'https://www.google.com/buzz/api/auth/OAuthAuthorizeToken?' +
138 "domain=#{oauth_client.client_credential_key}&" +
140 "xoauth_displayname=Google%20API%20Client"
142 oauth_client.fetch_temporary_credential!(:additional_parameters => {
144 :xoauth_displayname => 'Google API Client'
148 Launchy::Browser.run(oauth_client.authorization_uri.to_s)
151 oauth_client.fetch_token_credential!(:verifier => $verifier)
153 "client_credential_key" => oauth_client.client_credential_key,
154 "client_credential_secret" => oauth_client.client_credential_secret,
155 "token_credential_key" => oauth_client.token_credential_key,
156 "token_credential_secret" => oauth_client.token_credential_secret
158 config_file = File.expand_path('~/.google-api.yaml')
159 open(config_file, 'w') { |file| file.write(YAML.dump(config)) }
163 def execute(options={})
164 config_file = File.expand_path('~/.google-api.yaml')
165 signed = File.exist?(config_file)
166 rpcname = ARGV.detect { |p| p =~ /^[a-z0-9_-]+\.[a-z0-9_\.-]+$/i }
170 STDERR.puts('Could not find rpcname.')
173 service_name = options[:service_name] || rpcname[/^([^\.]+)\./, 1]
174 client = Google::APIClient.new(:service => service_name)
176 if !client.authorization.kind_of?(Signet::OAuth1::Client)
178 "Unexpected authorization mechanism: #{client.authorization.class}"
182 config = open(config_file, 'r') { |file| YAML.load(file.read) }
183 client.authorization.client_credential_key =
184 config["client_credential_key"]
185 client.authorization.client_credential_secret =
186 config["client_credential_secret"]
187 client.authorization.token_credential_key =
188 config["token_credential_key"]
189 client.authorization.token_credential_secret =
190 config["token_credential_secret"]
193 options[:service_version] || client.latest_service(service_name).version
194 service = client.discovered_service(service_name, service_version)
195 method = service.to_h[rpcname]
198 "Method #{rpcname} does not exist for " +
199 "#{service_name}-#{service_version}."
203 parameters = ARGV.inject({}) do |accu, pair|
204 name, value = pair.split('=', 2)
209 input_streams, _, _ = IO.select([STDIN], [], [], 0)
210 request_body = STDIN.read || '' if input_streams
212 if options[:content_type]
213 headers << ['Content-Type', options[:content_type]]
216 headers << ['Content-Type', 'application/json']
218 response = client.execute(
219 method, parameters, request_body, headers, {:signed => signed}
221 status, headers, body = response
227 STDERR.puts('API fuzzing not yet supported.')
229 # Fuzz just one method
231 # Fuzz the entire API
236 self.send(command.gsub(/-/, "_").to_sym, options)