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 "-s", "--service <name>", String,
96 "Perform discovery on service") do |s|
97 options[:service_name] = s
100 "--service-version <id>", String,
101 "Select service version") do |id|
102 options[:service_version] = id
105 "--content-type <format>", String,
106 "Content-Type for request") do |f|
107 # Resolve content type shortcuts
110 f = 'application/json'
112 f = 'application/xml'
114 f = 'application/atom+xml'
116 f = 'application/rss+xml'
118 options[:content_type] = f
121 opts.on("-v", "--verbose", "Run verbosely") do |v|
122 options[:verbose] = v
124 opts.on("-h", "--help", "Show this message") do
128 opts.on("--version", "Show version") do
129 puts "google-api-client (#{Google::APIClient::VERSION::STRING})"
134 "\nAvailable commands:\n" +
135 " oauth-login Log a user into an API\n" +
136 " list List the methods available for a service\n" +
137 " execute Execute a method on the API\n" +
138 " irb Start an interactive client session"
144 self.parser.parse!(self.argv)
145 self.send(self.command.gsub(/-/, "_").to_sym)
149 require 'signet/oauth_1/client'
153 logger = WEBrick::Log.new('/dev/null') # TODO(bobaman): Cross-platform?
154 server = WEBrick::HTTPServer.new(
155 :Port => OAUTH_SERVER_PORT,
159 trap("INT") { server.shutdown }
161 server.mount("/", OAuthVerifierServlet)
163 oauth_client = Signet::OAuth1::Client.new(
164 :temporary_credential_uri =>
165 'https://www.google.com/accounts/OAuthGetRequestToken',
166 :authorization_uri =>
167 'https://www.google.com/accounts/OAuthAuthorizeToken',
168 :token_credential_uri =>
169 'https://www.google.com/accounts/OAuthGetAccessToken',
170 :client_credential_key => 'anonymous',
171 :client_credential_secret => 'anonymous',
172 :callback => "http://localhost:#{OAUTH_SERVER_PORT}/"
174 scope = options[:scope]
177 when "https://www.googleapis.com/auth/buzz",
178 "https://www.googleapis.com/auth/buzz.readonly"
179 oauth_client.authorization_uri =
180 'https://www.google.com/buzz/api/auth/OAuthAuthorizeToken?' +
181 "domain=#{oauth_client.client_credential_key}&" +
183 "xoauth_displayname=Google%20API%20Client"
185 oauth_client.fetch_temporary_credential!(:additional_parameters => {
187 :xoauth_displayname => 'Google API Client'
191 Launchy::Browser.run(oauth_client.authorization_uri.to_s)
194 oauth_client.fetch_token_credential!(:verifier => $verifier)
197 "client_credential_key" => oauth_client.client_credential_key,
198 "client_credential_secret" => oauth_client.client_credential_secret,
199 "token_credential_key" => oauth_client.token_credential_key,
200 "token_credential_secret" => oauth_client.token_credential_secret
202 config_file = File.expand_path('~/.google-api.yaml')
203 open(config_file, 'w') { |file| file.write(YAML.dump(config)) }
208 service_name = options[:service_name]
209 client = Google::APIClient.new(
210 :service => service_name,
211 :authorization => nil
214 options[:service_version] ||
215 client.latest_service_version(service_name).version
216 service = client.discovered_service(service_name, service_version)
217 rpcnames = service.to_h.keys
218 puts rpcnames.sort.join("\n")
223 require 'signet/oauth_1/client'
225 config_file = File.expand_path('~/.google-api.yaml')
226 signed = File.exist?(config_file)
228 STDERR.puts('No rpcname supplied.')
231 service_name = options[:service_name] || self.rpcname[/^([^\.]+)\./, 1]
232 client = Google::APIClient.new(
233 :service => service_name,
234 :authorization => :oauth_1
237 if !client.authorization.kind_of?(Signet::OAuth1::Client)
239 "Unexpected authorization mechanism: " +
240 "#{client.authorization.class}"
244 config = open(config_file, 'r') { |file| YAML.load(file.read) }
245 client.authorization.client_credential_key =
246 config["client_credential_key"]
247 client.authorization.client_credential_secret =
248 config["client_credential_secret"]
249 client.authorization.token_credential_key =
250 config["token_credential_key"]
251 client.authorization.token_credential_secret =
252 config["token_credential_secret"]
255 options[:service_version] ||
256 client.latest_service_version(service_name).version
257 service = client.discovered_service(service_name, service_version)
258 method = service.to_h[self.rpcname]
261 "Method #{self.rpcname} does not exist for " +
262 "#{service_name}-#{service_version}."
266 parameters = self.argv.inject({}) do |accu, pair|
267 name, value = pair.split('=', 2)
272 input_streams, _, _ = IO.select([STDIN], [], [], 0)
273 request_body = STDIN.read || '' if input_streams
275 if options[:content_type]
276 headers << ['Content-Type', options[:content_type]]
279 headers << ['Content-Type', 'application/json']
282 response = client.execute(
283 method, parameters, request_body, headers, {:signed => signed}
285 status, headers, body = response
288 rescue ArgumentError => e
295 require 'signet/oauth_1/client'
298 config_file = File.expand_path('~/.google-api.yaml')
299 signed = File.exist?(config_file)
301 $client = Google::APIClient.new(
302 :service => options[:service_name],
303 :authorization => (signed ? :oauth_1 : nil)
307 if $client.authorization &&
308 !$client.authorization.kind_of?(Signet::OAuth1::Client)
310 "Unexpected authorization mechanism: " +
311 "#{$client.authorization.class}"
315 config = open(config_file, 'r') { |file| YAML.load(file.read) }
316 $client.authorization.client_credential_key =
317 config["client_credential_key"]
318 $client.authorization.client_credential_secret =
319 config["client_credential_secret"]
320 $client.authorization.token_credential_key =
321 config["token_credential_key"]
322 $client.authorization.token_credential_secret =
323 config["token_credential_secret"]
326 # Otherwise IRB will misinterpret command-line options
332 STDERR.puts('API fuzzing not yet supported.')
334 # Fuzz just one method
336 # Fuzz the entire API
349 Google::APIClient::CLI.new(ARGV).parse!