2010-10-12 01:53:40 +00:00
|
|
|
#!/usr/bin/env ruby
|
|
|
|
|
|
|
|
bin_dir = File.expand_path("..", __FILE__)
|
|
|
|
lib_dir = File.expand_path("../lib", bin_dir)
|
|
|
|
|
|
|
|
$LOAD_PATH.unshift(lib_dir)
|
|
|
|
$LOAD_PATH.uniq!
|
|
|
|
|
|
|
|
OAUTH_SERVER_PORT = 12736
|
|
|
|
|
|
|
|
require 'rubygems'
|
|
|
|
require 'optparse'
|
2010-10-20 23:49:15 +00:00
|
|
|
require 'httpadapter'
|
|
|
|
require 'webrick'
|
2010-10-12 01:53:40 +00:00
|
|
|
require 'google/api_client/version'
|
|
|
|
require 'google/api_client'
|
|
|
|
|
|
|
|
ARGV.unshift('--help') if ARGV.empty?
|
|
|
|
|
2010-10-20 23:49:15 +00:00
|
|
|
module Google
|
|
|
|
class APIClient
|
|
|
|
class CLI
|
|
|
|
# Used for oauth login
|
|
|
|
class OAuthVerifierServlet < WEBrick::HTTPServlet::AbstractServlet
|
|
|
|
attr_reader :verifier
|
2010-10-12 01:53:40 +00:00
|
|
|
|
2010-10-20 23:49:15 +00:00
|
|
|
def do_GET(request, response)
|
|
|
|
$verifier ||= Addressable::URI.unencode_component(
|
|
|
|
request.request_uri.to_s[/\?.*oauth_verifier=([^&$]+)(&|$)/, 1]
|
|
|
|
)
|
|
|
|
response.status = WEBrick::HTTPStatus::RC_ACCEPTED
|
|
|
|
# This javascript will auto-close the tab after the
|
|
|
|
# verifier is obtained.
|
|
|
|
response.body = <<-HTML
|
2010-10-12 01:53:40 +00:00
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<script>
|
|
|
|
function closeWindow() {
|
|
|
|
window.open('', '_self', '');
|
|
|
|
window.close();
|
|
|
|
}
|
|
|
|
setTimeout(closeWindow, 10);
|
|
|
|
</script>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
You may close this window.
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
HTML
|
2010-10-20 23:49:15 +00:00
|
|
|
# Eww, hack!
|
|
|
|
server = self.instance_variable_get('@server')
|
|
|
|
server.stop if server
|
|
|
|
end
|
|
|
|
end
|
2010-10-12 01:53:40 +00:00
|
|
|
|
2010-10-20 23:49:15 +00:00
|
|
|
# Initialize with default parameter values
|
|
|
|
def initialize(argv)
|
|
|
|
@options = {
|
|
|
|
:command => 'execute',
|
|
|
|
:rpcname => nil,
|
|
|
|
:verbose => false
|
|
|
|
}
|
|
|
|
@argv = argv.clone
|
2010-10-22 00:36:15 +00:00
|
|
|
if @argv.first =~ /^[a-z0-9][a-z0-9_-]*$/i
|
2010-10-20 23:49:15 +00:00
|
|
|
self.options[:command] = @argv.shift
|
|
|
|
end
|
|
|
|
if @argv.first =~ /^[a-z0-9_-]+\.[a-z0-9_\.-]+$/i
|
|
|
|
self.options[:rpcname] = @argv.shift
|
|
|
|
end
|
|
|
|
end
|
2010-10-12 01:53:40 +00:00
|
|
|
|
2010-10-20 23:49:15 +00:00
|
|
|
attr_reader :options
|
|
|
|
attr_reader :argv
|
2010-10-12 01:53:40 +00:00
|
|
|
|
2010-10-20 23:49:15 +00:00
|
|
|
def command
|
|
|
|
return self.options[:command]
|
|
|
|
end
|
2010-10-12 01:53:40 +00:00
|
|
|
|
2010-10-20 23:49:15 +00:00
|
|
|
def rpcname
|
|
|
|
return self.options[:rpcname]
|
|
|
|
end
|
2010-10-12 01:53:40 +00:00
|
|
|
|
2010-10-20 23:49:15 +00:00
|
|
|
def parser
|
|
|
|
@parser ||= OptionParser.new do |opts|
|
|
|
|
opts.banner = "Usage: google-api " +
|
|
|
|
"(execute <rpcname> | [command]) [options] [-- <parameters>]"
|
2010-10-12 01:53:40 +00:00
|
|
|
|
2010-10-22 00:36:15 +00:00
|
|
|
opts.separator "\nAvailable options:"
|
2010-10-12 01:53:40 +00:00
|
|
|
|
2010-10-20 23:49:15 +00:00
|
|
|
opts.on(
|
|
|
|
"--scope <scope>", String, "Set the OAuth scope") do |s|
|
|
|
|
options[:scope] = s
|
|
|
|
end
|
2010-10-22 00:36:23 +00:00
|
|
|
opts.on(
|
|
|
|
"--client-key <key>", String,
|
|
|
|
"Set the 2-legged OAuth key") do |k|
|
|
|
|
options[:client_credential_key] = k
|
|
|
|
end
|
|
|
|
opts.on(
|
|
|
|
"--client-secret <secret>", String,
|
|
|
|
"Set the 2-legged OAuth secret") do |s|
|
|
|
|
options[:client_credential_secret] = s
|
|
|
|
end
|
2010-10-20 23:49:15 +00:00
|
|
|
opts.on(
|
|
|
|
"-s", "--service <name>", String,
|
|
|
|
"Perform discovery on service") do |s|
|
|
|
|
options[:service_name] = s
|
|
|
|
end
|
|
|
|
opts.on(
|
|
|
|
"--service-version <id>", String,
|
|
|
|
"Select service version") do |id|
|
|
|
|
options[:service_version] = id
|
|
|
|
end
|
|
|
|
opts.on(
|
|
|
|
"--content-type <format>", String,
|
|
|
|
"Content-Type for request") do |f|
|
|
|
|
# Resolve content type shortcuts
|
|
|
|
case f
|
|
|
|
when 'json'
|
|
|
|
f = 'application/json'
|
|
|
|
when 'xml'
|
|
|
|
f = 'application/xml'
|
|
|
|
when 'atom'
|
|
|
|
f = 'application/atom+xml'
|
|
|
|
when 'rss'
|
|
|
|
f = 'application/rss+xml'
|
|
|
|
end
|
|
|
|
options[:content_type] = f
|
|
|
|
end
|
2010-10-22 00:36:23 +00:00
|
|
|
opts.on(
|
|
|
|
"-u", "--uri <uri>", String,
|
|
|
|
"Sets the URI to perform a request against") do |u|
|
|
|
|
options[:uri] = u
|
|
|
|
end
|
|
|
|
opts.on(
|
|
|
|
"-m", "--method <method>", String,
|
|
|
|
"Sets the HTTP method to use for the request") do |m|
|
|
|
|
options[:http_method] = m
|
|
|
|
end
|
2010-10-13 00:57:04 +00:00
|
|
|
|
2010-10-22 00:36:15 +00:00
|
|
|
opts.on("-v", "--verbose", "Run verbosely") do |v|
|
2010-10-20 23:49:15 +00:00
|
|
|
options[:verbose] = v
|
|
|
|
end
|
2010-10-22 00:36:15 +00:00
|
|
|
opts.on("-h", "--help", "Show this message") do
|
2010-10-20 23:49:15 +00:00
|
|
|
puts opts
|
|
|
|
exit
|
|
|
|
end
|
2010-10-22 00:36:15 +00:00
|
|
|
opts.on("--version", "Show version") do
|
2010-10-20 23:49:15 +00:00
|
|
|
puts "google-api-client (#{Google::APIClient::VERSION::STRING})"
|
|
|
|
exit
|
|
|
|
end
|
2010-10-22 00:36:15 +00:00
|
|
|
|
|
|
|
opts.separator(
|
|
|
|
"\nAvailable commands:\n" +
|
2010-10-22 00:36:20 +00:00
|
|
|
" oauth-login Log a user into an API\n" +
|
|
|
|
" list List the methods available for a service\n" +
|
|
|
|
" execute Execute a method on the API\n" +
|
|
|
|
" irb Start an interactive client session"
|
2010-10-22 00:36:15 +00:00
|
|
|
)
|
2010-10-20 23:49:15 +00:00
|
|
|
end
|
|
|
|
end
|
2010-10-13 00:57:04 +00:00
|
|
|
|
2010-10-20 23:49:15 +00:00
|
|
|
def parse!
|
|
|
|
self.parser.parse!(self.argv)
|
2010-10-22 00:36:23 +00:00
|
|
|
symbol = self.command.gsub(/-/, "_").to_sym
|
|
|
|
if !COMMANDS.include?(symbol)
|
|
|
|
STDERR.puts("Invalid command: #{self.command}")
|
|
|
|
exit(1)
|
|
|
|
end
|
|
|
|
self.send(symbol)
|
2010-10-20 23:49:15 +00:00
|
|
|
end
|
2010-10-13 00:57:04 +00:00
|
|
|
|
2010-10-22 00:36:23 +00:00
|
|
|
COMMANDS = [
|
|
|
|
:oauth_login,
|
|
|
|
:list,
|
|
|
|
:execute,
|
|
|
|
:irb,
|
|
|
|
:fuzz
|
|
|
|
]
|
|
|
|
|
2010-10-20 23:49:15 +00:00
|
|
|
def oauth_login
|
|
|
|
require 'signet/oauth_1/client'
|
|
|
|
require 'launchy'
|
|
|
|
require 'yaml'
|
2010-10-22 00:36:23 +00:00
|
|
|
if options[:client_credential_key] &&
|
|
|
|
options[:client_credential_secret]
|
|
|
|
scope = options[:scope]
|
|
|
|
config = {
|
|
|
|
"scope" => nil,
|
|
|
|
"client_credential_key" => options[:client_credential_key],
|
|
|
|
"client_credential_secret" => options[:client_credential_secret],
|
|
|
|
"token_credential_key" => nil,
|
|
|
|
"token_credential_secret" => nil
|
|
|
|
}
|
|
|
|
config_file = File.expand_path('~/.google-api.yaml')
|
|
|
|
open(config_file, 'w') { |file| file.write(YAML.dump(config)) }
|
|
|
|
exit(0)
|
|
|
|
else
|
|
|
|
$verifier = nil
|
|
|
|
# TODO(bobaman): Cross-platform?
|
|
|
|
logger = WEBrick::Log.new('/dev/null')
|
|
|
|
server = WEBrick::HTTPServer.new(
|
|
|
|
:Port => OAUTH_SERVER_PORT,
|
|
|
|
:Logger => logger,
|
|
|
|
:AccessLog => logger
|
|
|
|
)
|
|
|
|
trap("INT") { server.shutdown }
|
2010-10-20 23:49:15 +00:00
|
|
|
|
2010-10-22 00:36:23 +00:00
|
|
|
server.mount("/", OAuthVerifierServlet)
|
2010-10-13 00:57:04 +00:00
|
|
|
|
2010-10-22 00:36:23 +00:00
|
|
|
oauth_client = Signet::OAuth1::Client.new(
|
|
|
|
:temporary_credential_uri =>
|
|
|
|
'https://www.google.com/accounts/OAuthGetRequestToken',
|
|
|
|
:authorization_uri =>
|
|
|
|
'https://www.google.com/accounts/OAuthAuthorizeToken',
|
|
|
|
:token_credential_uri =>
|
|
|
|
'https://www.google.com/accounts/OAuthGetAccessToken',
|
|
|
|
:client_credential_key => 'anonymous',
|
|
|
|
:client_credential_secret => 'anonymous',
|
|
|
|
:callback => "http://localhost:#{OAUTH_SERVER_PORT}/"
|
|
|
|
)
|
|
|
|
scope = options[:scope]
|
|
|
|
# Special cases
|
|
|
|
case scope
|
|
|
|
when "https://www.googleapis.com/auth/buzz",
|
|
|
|
"https://www.googleapis.com/auth/buzz.readonly"
|
|
|
|
oauth_client.authorization_uri =
|
|
|
|
'https://www.google.com/buzz/api/auth/OAuthAuthorizeToken?' +
|
|
|
|
"domain=#{oauth_client.client_credential_key}&" +
|
|
|
|
"scope=#{scope}&" +
|
|
|
|
"xoauth_displayname=Google%20API%20Client"
|
|
|
|
end
|
|
|
|
oauth_client.fetch_temporary_credential!(:additional_parameters => {
|
|
|
|
:scope => scope,
|
|
|
|
:xoauth_displayname => 'Google API Client'
|
|
|
|
})
|
2010-10-20 23:49:15 +00:00
|
|
|
|
2010-10-22 00:36:23 +00:00
|
|
|
# Launch browser
|
|
|
|
Launchy::Browser.run(oauth_client.authorization_uri.to_s)
|
2010-10-20 23:49:15 +00:00
|
|
|
|
2010-10-22 00:36:23 +00:00
|
|
|
server.start
|
|
|
|
oauth_client.fetch_token_credential!(:verifier => $verifier)
|
|
|
|
config = {
|
|
|
|
"scope" => scope,
|
|
|
|
"client_credential_key" =>
|
|
|
|
oauth_client.client_credential_key,
|
|
|
|
"client_credential_secret" =>
|
|
|
|
oauth_client.client_credential_secret,
|
|
|
|
"token_credential_key" =>
|
|
|
|
oauth_client.token_credential_key,
|
|
|
|
"token_credential_secret" =>
|
|
|
|
oauth_client.token_credential_secret
|
|
|
|
}
|
|
|
|
config_file = File.expand_path('~/.google-api.yaml')
|
|
|
|
open(config_file, 'w') { |file| file.write(YAML.dump(config)) }
|
|
|
|
exit(0)
|
|
|
|
end
|
2010-10-20 23:49:15 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def list
|
|
|
|
service_name = options[:service_name]
|
|
|
|
client = Google::APIClient.new(
|
|
|
|
:service => service_name,
|
|
|
|
:authorization => nil
|
|
|
|
)
|
|
|
|
service_version =
|
|
|
|
options[:service_version] ||
|
|
|
|
client.latest_service_version(service_name).version
|
|
|
|
service = client.discovered_service(service_name, service_version)
|
|
|
|
rpcnames = service.to_h.keys
|
|
|
|
puts rpcnames.sort.join("\n")
|
|
|
|
exit(0)
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
|
|
|
require 'signet/oauth_1/client'
|
|
|
|
require 'yaml'
|
|
|
|
config_file = File.expand_path('~/.google-api.yaml')
|
|
|
|
signed = File.exist?(config_file)
|
2010-10-22 00:36:23 +00:00
|
|
|
|
|
|
|
# Setup HTTP request data
|
|
|
|
request_body = ''
|
|
|
|
input_streams, _, _ = IO.select([STDIN], [], [], 0)
|
|
|
|
request_body = STDIN.read || '' if input_streams
|
|
|
|
headers = []
|
|
|
|
if options[:content_type]
|
|
|
|
headers << ['Content-Type', options[:content_type]]
|
|
|
|
elsif request_body
|
|
|
|
# Default to JSON
|
|
|
|
headers << ['Content-Type', 'application/json']
|
2010-10-20 23:49:15 +00:00
|
|
|
end
|
2010-10-22 00:36:23 +00:00
|
|
|
|
|
|
|
configure_authorization = lambda do |client|
|
2010-10-20 23:49:15 +00:00
|
|
|
if !client.authorization.kind_of?(Signet::OAuth1::Client)
|
|
|
|
STDERR.puts(
|
|
|
|
"Unexpected authorization mechanism: " +
|
|
|
|
"#{client.authorization.class}"
|
|
|
|
)
|
|
|
|
exit(1)
|
|
|
|
end
|
|
|
|
config = open(config_file, 'r') { |file| YAML.load(file.read) }
|
|
|
|
client.authorization.client_credential_key =
|
|
|
|
config["client_credential_key"]
|
|
|
|
client.authorization.client_credential_secret =
|
|
|
|
config["client_credential_secret"]
|
|
|
|
client.authorization.token_credential_key =
|
|
|
|
config["token_credential_key"]
|
|
|
|
client.authorization.token_credential_secret =
|
|
|
|
config["token_credential_secret"]
|
|
|
|
end
|
2010-10-22 00:36:23 +00:00
|
|
|
|
|
|
|
if options[:uri]
|
|
|
|
# Make request with URI manually specified
|
|
|
|
uri = Addressable::URI.parse(options[:uri])
|
|
|
|
if uri.relative?
|
|
|
|
STDERR.puts('URI may not be relative.')
|
|
|
|
exit(1)
|
|
|
|
end
|
|
|
|
method = options[:http_method]
|
|
|
|
method ||= request_body == '' ? 'GET' : 'POST'
|
|
|
|
method.upcase!
|
|
|
|
client = Google::APIClient.new(:authorization => :two_legged_oauth_1)
|
|
|
|
configure_authorization.call(client) if signed
|
|
|
|
request = [method, uri.to_str, headers, [request_body]]
|
|
|
|
request = client.sign_request(request)
|
|
|
|
response = client.transmit_request(request)
|
2010-10-20 23:49:15 +00:00
|
|
|
status, headers, body = response
|
|
|
|
puts body
|
|
|
|
exit(0)
|
2010-10-22 00:36:23 +00:00
|
|
|
else
|
|
|
|
# Make request with URI generated from template and parameters
|
|
|
|
if !self.rpcname
|
|
|
|
STDERR.puts('No rpcname supplied.')
|
|
|
|
exit(1)
|
|
|
|
end
|
|
|
|
service_name =
|
|
|
|
options[:service_name] || self.rpcname[/^([^\.]+)\./, 1]
|
|
|
|
client = Google::APIClient.new(
|
|
|
|
:service => service_name,
|
|
|
|
:authorization => :oauth_1
|
|
|
|
)
|
|
|
|
configure_authorization.call(client) if signed
|
|
|
|
service_version =
|
|
|
|
options[:service_version] ||
|
|
|
|
client.latest_service_version(service_name).version
|
|
|
|
service = client.discovered_service(service_name, service_version)
|
|
|
|
method = service.to_h[self.rpcname]
|
|
|
|
if !method
|
|
|
|
STDERR.puts(
|
|
|
|
"Method #{self.rpcname} does not exist for " +
|
|
|
|
"#{service_name}-#{service_version}."
|
|
|
|
)
|
|
|
|
exit(1)
|
|
|
|
end
|
|
|
|
parameters = self.argv.inject({}) do |accu, pair|
|
|
|
|
name, value = pair.split('=', 2)
|
|
|
|
accu[name] = value
|
|
|
|
accu
|
|
|
|
end
|
|
|
|
begin
|
|
|
|
response = client.execute(
|
|
|
|
method, parameters, request_body, headers, {:signed => signed}
|
|
|
|
)
|
|
|
|
status, headers, body = response
|
|
|
|
puts body
|
|
|
|
exit(0)
|
|
|
|
rescue ArgumentError => e
|
|
|
|
puts e.message
|
|
|
|
exit(1)
|
|
|
|
end
|
2010-10-20 23:49:15 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-10-22 00:36:15 +00:00
|
|
|
def irb
|
2010-10-20 23:49:15 +00:00
|
|
|
require 'signet/oauth_1/client'
|
|
|
|
require 'yaml'
|
2010-10-22 00:36:15 +00:00
|
|
|
require 'irb'
|
2010-10-20 23:49:15 +00:00
|
|
|
config_file = File.expand_path('~/.google-api.yaml')
|
|
|
|
signed = File.exist?(config_file)
|
|
|
|
|
|
|
|
$client = Google::APIClient.new(
|
|
|
|
:service => options[:service_name],
|
|
|
|
:authorization => (signed ? :oauth_1 : nil)
|
|
|
|
)
|
|
|
|
|
|
|
|
if signed
|
|
|
|
if $client.authorization &&
|
|
|
|
!$client.authorization.kind_of?(Signet::OAuth1::Client)
|
|
|
|
STDERR.puts(
|
|
|
|
"Unexpected authorization mechanism: " +
|
|
|
|
"#{$client.authorization.class}"
|
|
|
|
)
|
|
|
|
exit(1)
|
|
|
|
end
|
|
|
|
config = open(config_file, 'r') { |file| YAML.load(file.read) }
|
|
|
|
$client.authorization.client_credential_key =
|
|
|
|
config["client_credential_key"]
|
|
|
|
$client.authorization.client_credential_secret =
|
|
|
|
config["client_credential_secret"]
|
|
|
|
$client.authorization.token_credential_key =
|
|
|
|
config["token_credential_key"]
|
|
|
|
$client.authorization.token_credential_secret =
|
|
|
|
config["token_credential_secret"]
|
|
|
|
end
|
|
|
|
|
2010-10-22 00:36:15 +00:00
|
|
|
# Otherwise IRB will misinterpret command-line options
|
|
|
|
ARGV.clear
|
2010-10-20 23:49:15 +00:00
|
|
|
IRB.start(__FILE__)
|
|
|
|
end
|
|
|
|
|
|
|
|
def fuzz
|
|
|
|
STDERR.puts('API fuzzing not yet supported.')
|
|
|
|
if self.rpcname
|
|
|
|
# Fuzz just one method
|
|
|
|
else
|
|
|
|
# Fuzz the entire API
|
|
|
|
end
|
|
|
|
exit(1)
|
|
|
|
end
|
2010-10-22 00:36:15 +00:00
|
|
|
|
|
|
|
def help
|
|
|
|
puts self.parser
|
|
|
|
exit(0)
|
|
|
|
end
|
2010-10-20 23:49:15 +00:00
|
|
|
end
|
2010-10-12 01:53:40 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-10-20 23:49:15 +00:00
|
|
|
Google::APIClient::CLI.new(ARGV).parse!
|