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_-]+$/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>]"
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_tail("-v", "--verbose", "Run verbosely") do |v|
122 options[:verbose] = v
124 opts.on_tail("-h", "--help", "Show this message") do
128 opts.on_tail("--version", "Show version") do
129 puts "google-api-client (#{Google::APIClient::VERSION::STRING})"
136 self.parser.parse!(self.argv)
137 self.send(self.command.gsub(/-/, "_").to_sym)
141 require 'signet/oauth_1/client'
145 logger = WEBrick::Log.new('/dev/null') # TODO(bobaman): Cross-platform?
146 server = WEBrick::HTTPServer.new(
147 :Port => OAUTH_SERVER_PORT,
151 trap("INT") { server.shutdown }
153 server.mount("/", OAuthVerifierServlet)
155 oauth_client = Signet::OAuth1::Client.new(
156 :temporary_credential_uri =>
157 'https://www.google.com/accounts/OAuthGetRequestToken',
158 :authorization_uri =>
159 'https://www.google.com/accounts/OAuthAuthorizeToken',
160 :token_credential_uri =>
161 'https://www.google.com/accounts/OAuthGetAccessToken',
162 :client_credential_key => 'anonymous',
163 :client_credential_secret => 'anonymous',
164 :callback => "http://localhost:#{OAUTH_SERVER_PORT}/"
166 scope = options[:scope]
169 when "https://www.googleapis.com/auth/buzz",
170 "https://www.googleapis.com/auth/buzz.readonly"
171 oauth_client.authorization_uri =
172 'https://www.google.com/buzz/api/auth/OAuthAuthorizeToken?' +
173 "domain=#{oauth_client.client_credential_key}&" +
175 "xoauth_displayname=Google%20API%20Client"
177 oauth_client.fetch_temporary_credential!(:additional_parameters => {
179 :xoauth_displayname => 'Google API Client'
183 Launchy::Browser.run(oauth_client.authorization_uri.to_s)
186 oauth_client.fetch_token_credential!(:verifier => $verifier)
189 "client_credential_key" => oauth_client.client_credential_key,
190 "client_credential_secret" => oauth_client.client_credential_secret,
191 "token_credential_key" => oauth_client.token_credential_key,
192 "token_credential_secret" => oauth_client.token_credential_secret
194 config_file = File.expand_path('~/.google-api.yaml')
195 open(config_file, 'w') { |file| file.write(YAML.dump(config)) }
200 service_name = options[:service_name]
201 client = Google::APIClient.new(
202 :service => service_name,
203 :authorization => nil
206 options[:service_version] ||
207 client.latest_service_version(service_name).version
208 service = client.discovered_service(service_name, service_version)
209 rpcnames = service.to_h.keys
210 puts rpcnames.sort.join("\n")
215 require 'signet/oauth_1/client'
217 config_file = File.expand_path('~/.google-api.yaml')
218 signed = File.exist?(config_file)
220 STDERR.puts('No rpcname supplied.')
223 service_name = options[:service_name] || self.rpcname[/^([^\.]+)\./, 1]
224 client = Google::APIClient.new(
225 :service => service_name,
226 :authorization => :oauth_1
229 if !client.authorization.kind_of?(Signet::OAuth1::Client)
231 "Unexpected authorization mechanism: " +
232 "#{client.authorization.class}"
236 config = open(config_file, 'r') { |file| YAML.load(file.read) }
237 client.authorization.client_credential_key =
238 config["client_credential_key"]
239 client.authorization.client_credential_secret =
240 config["client_credential_secret"]
241 client.authorization.token_credential_key =
242 config["token_credential_key"]
243 client.authorization.token_credential_secret =
244 config["token_credential_secret"]
247 options[:service_version] ||
248 client.latest_service_version(service_name).version
249 service = client.discovered_service(service_name, service_version)
250 method = service.to_h[self.rpcname]
253 "Method #{self.rpcname} does not exist for " +
254 "#{service_name}-#{service_version}."
258 parameters = self.argv.inject({}) do |accu, pair|
259 name, value = pair.split('=', 2)
264 input_streams, _, _ = IO.select([STDIN], [], [], 0)
265 request_body = STDIN.read || '' if input_streams
267 if options[:content_type]
268 headers << ['Content-Type', options[:content_type]]
271 headers << ['Content-Type', 'application/json']
274 response = client.execute(
275 method, parameters, request_body, headers, {:signed => signed}
277 status, headers, body = response
280 rescue ArgumentError => e
287 require 'signet/oauth_1/client'
289 config_file = File.expand_path('~/.google-api.yaml')
290 signed = File.exist?(config_file)
292 $client = Google::APIClient.new(
293 :service => options[:service_name],
294 :authorization => (signed ? :oauth_1 : nil)
298 if $client.authorization &&
299 !$client.authorization.kind_of?(Signet::OAuth1::Client)
301 "Unexpected authorization mechanism: " +
302 "#{$client.authorization.class}"
306 config = open(config_file, 'r') { |file| YAML.load(file.read) }
307 $client.authorization.client_credential_key =
308 config["client_credential_key"]
309 $client.authorization.client_credential_secret =
310 config["client_credential_secret"]
311 $client.authorization.token_credential_key =
312 config["token_credential_key"]
313 $client.authorization.token_credential_secret =
314 config["token_credential_secret"]
322 STDERR.puts('API fuzzing not yet supported.')
324 # Fuzz just one method
326 # Fuzz the entire API
334 Google::APIClient::CLI.new(ARGV).parse!