google-api-client 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +3 -0
- data/LICENSE +202 -0
- data/README +68 -0
- data/Rakefile +52 -0
- data/bin/google-api +295 -0
- data/lib/google/api_client.rb +375 -0
- data/lib/google/api_client/discovery.rb +535 -0
- data/lib/google/api_client/parsers/json_parser.rb +40 -0
- data/lib/google/api_client/version.rb +25 -0
- data/spec/google/api_client/discovery_spec.rb +428 -0
- data/spec/google/api_client/parsers/json_parser_spec.rb +51 -0
- data/spec/google/api_client_spec.rb +79 -0
- data/spec/spec.opts +2 -0
- data/spec/spec_helper.rb +5 -0
- data/tasks/clobber.rake +2 -0
- data/tasks/gem.rake +76 -0
- data/tasks/git.rake +40 -0
- data/tasks/metrics.rake +22 -0
- data/tasks/rdoc.rake +26 -0
- data/tasks/spec.rake +78 -0
- data/tasks/yard.rake +26 -0
- metadata +281 -0
data/CHANGELOG
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,202 @@
|
|
1
|
+
|
2
|
+
Apache License
|
3
|
+
Version 2.0, January 2004
|
4
|
+
http://www.apache.org/licenses/
|
5
|
+
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
7
|
+
|
8
|
+
1. Definitions.
|
9
|
+
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
12
|
+
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
14
|
+
the copyright owner that is granting the License.
|
15
|
+
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
17
|
+
other entities that control, are controlled by, or are under common
|
18
|
+
control with that entity. For the purposes of this definition,
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
20
|
+
direction or management of such entity, whether by contract or
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
23
|
+
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
25
|
+
exercising permissions granted by this License.
|
26
|
+
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
28
|
+
including but not limited to software source code, documentation
|
29
|
+
source, and configuration files.
|
30
|
+
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
32
|
+
transformation or translation of a Source form, including but
|
33
|
+
not limited to compiled object code, generated documentation,
|
34
|
+
and conversions to other media types.
|
35
|
+
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
37
|
+
Object form, made available under the License, as indicated by a
|
38
|
+
copyright notice that is included in or attached to the work
|
39
|
+
(an example is provided in the Appendix below).
|
40
|
+
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
47
|
+
the Work and Derivative Works thereof.
|
48
|
+
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
50
|
+
the original version of the Work and any modifications or additions
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
62
|
+
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
65
|
+
subsequently incorporated within the Work.
|
66
|
+
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
73
|
+
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
79
|
+
where such license applies only to those patent claims licensable
|
80
|
+
by such Contributor that are necessarily infringed by their
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
83
|
+
institute patent litigation against any entity (including a
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
86
|
+
or contributory patent infringement, then any patent licenses
|
87
|
+
granted to You under this License for that Work shall terminate
|
88
|
+
as of the date such litigation is filed.
|
89
|
+
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
92
|
+
modifications, and in Source or Object form, provided that You
|
93
|
+
meet the following conditions:
|
94
|
+
|
95
|
+
(a) You must give any other recipients of the Work or
|
96
|
+
Derivative Works a copy of this License; and
|
97
|
+
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
99
|
+
stating that You changed the files; and
|
100
|
+
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
103
|
+
attribution notices from the Source form of the Work,
|
104
|
+
excluding those notices that do not pertain to any part of
|
105
|
+
the Derivative Works; and
|
106
|
+
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
109
|
+
include a readable copy of the attribution notices contained
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
112
|
+
of the following places: within a NOTICE text file distributed
|
113
|
+
as part of the Derivative Works; within the Source form or
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
115
|
+
within a display generated by the Derivative Works, if and
|
116
|
+
wherever such third-party notices normally appear. The contents
|
117
|
+
of the NOTICE file are for informational purposes only and
|
118
|
+
do not modify the License. You may add Your own attribution
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
121
|
+
that such additional attribution notices cannot be construed
|
122
|
+
as modifying the License.
|
123
|
+
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
125
|
+
may provide additional or different license terms and conditions
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
129
|
+
the conditions stated in this License.
|
130
|
+
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
134
|
+
this License, without any additional terms or conditions.
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
136
|
+
the terms of any separate license agreement you may have executed
|
137
|
+
with Licensor regarding such Contributions.
|
138
|
+
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
141
|
+
except as required for reasonable and customary use in describing the
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
143
|
+
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
153
|
+
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
159
|
+
incidental, or consequential damages of any character arising as a
|
160
|
+
result of this License or out of the use or inability to use the
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
163
|
+
other commercial damages or losses), even if such Contributor
|
164
|
+
has been advised of the possibility of such damages.
|
165
|
+
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
169
|
+
or other liability obligations and/or rights consistent with this
|
170
|
+
License. However, in accepting such obligations, You may act only
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
175
|
+
of your accepting any such warranty or additional liability.
|
176
|
+
|
177
|
+
END OF TERMS AND CONDITIONS
|
178
|
+
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
180
|
+
|
181
|
+
To apply the Apache License to your work, attach the following
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
183
|
+
replaced with your own identifying information. (Don't include
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
185
|
+
comment syntax for the file format. We also recommend that a
|
186
|
+
file or class name and description of purpose be included on the
|
187
|
+
same "printed page" as the copyright notice for easier
|
188
|
+
identification within third-party archives.
|
189
|
+
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
191
|
+
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
193
|
+
you may not use this file except in compliance with the License.
|
194
|
+
You may obtain a copy of the License at
|
195
|
+
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
197
|
+
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
201
|
+
See the License for the specific language governing permissions and
|
202
|
+
limitations under the License.
|
data/README
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
== APIClient
|
2
|
+
|
3
|
+
Homepage:: google-api-ruby-client[http://code.google.com/p/google-api-ruby-client/]
|
4
|
+
Authors:: Bob Aman (mailto:bobaman@google.com), Matt Pokrzywa (mailto:mattpok@google.com)
|
5
|
+
Copyright:: Copyright 2010 Google Inc.
|
6
|
+
License:: Apache 2.0
|
7
|
+
|
8
|
+
== Description
|
9
|
+
|
10
|
+
The Google API Ruby Client makes it trivial to discover and access supported
|
11
|
+
APIs.
|
12
|
+
|
13
|
+
== Example Usage
|
14
|
+
|
15
|
+
# Initialize the client
|
16
|
+
require 'google/api_client'
|
17
|
+
require 'signet/oauth_1/client'
|
18
|
+
client = Google::APIClient.new(
|
19
|
+
:service => 'buzz',
|
20
|
+
# Buzz has API-specific endpoints
|
21
|
+
:authorization => Signet::OAuth1::Client.new(
|
22
|
+
:temporary_credential_uri =>
|
23
|
+
'https://www.google.com/accounts/OAuthGetRequestToken',
|
24
|
+
:authorization_uri =>
|
25
|
+
'https://www.google.com/buzz/api/auth/OAuthAuthorizeToken',
|
26
|
+
:token_credential_uri =>
|
27
|
+
'https://www.google.com/accounts/OAuthGetAccessToken',
|
28
|
+
:client_credential_key => 'anonymous',
|
29
|
+
:client_credential_secret => 'anonymous'
|
30
|
+
)
|
31
|
+
)
|
32
|
+
client.authorization.fetch_temporary_credential!(
|
33
|
+
:additional_parameters => {
|
34
|
+
'scope' => 'https://www.googleapis.com/auth/buzz'
|
35
|
+
}
|
36
|
+
)
|
37
|
+
redirect_uri = client.authorization.authorization_uri(
|
38
|
+
:additional_parameters => {
|
39
|
+
'domain' => client.authorization.client_credential_key,
|
40
|
+
'scope' => 'https://www.googleapis.com/auth/buzz'
|
41
|
+
}
|
42
|
+
)
|
43
|
+
# Redirect user here
|
44
|
+
client.authorization.fetch_token_credential!(:verifier => '12345')
|
45
|
+
|
46
|
+
# Discover available methods
|
47
|
+
method_names = client.discovered_service('buzz').to_h.keys
|
48
|
+
|
49
|
+
# Make an API call
|
50
|
+
response = client.execute(
|
51
|
+
'chili.activities.list',
|
52
|
+
{'scope' => '@self', 'userId' => '@me', 'alt' => 'json'}
|
53
|
+
)
|
54
|
+
status, headers, body = response
|
55
|
+
|
56
|
+
== Install
|
57
|
+
|
58
|
+
Be sure both http://gems.github.com/ and http://rubygems.org/ are in your gem
|
59
|
+
sources.
|
60
|
+
|
61
|
+
For normal client usage, this is sufficient:
|
62
|
+
|
63
|
+
sudo gem install google-api-client
|
64
|
+
|
65
|
+
The command line interface, the example applications, and the test suite
|
66
|
+
require additional dependencies. These may be obtained with:
|
67
|
+
|
68
|
+
sudo gem install google-api-client --development --force --no-rdoc --no-ri
|
data/Rakefile
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
lib_dir = File.expand_path('../lib', __FILE__)
|
2
|
+
$LOAD_PATH.unshift(lib_dir)
|
3
|
+
$LOAD_PATH.uniq!
|
4
|
+
|
5
|
+
require 'rubygems'
|
6
|
+
require 'rake'
|
7
|
+
require 'rake/testtask'
|
8
|
+
require 'rake/rdoctask'
|
9
|
+
require 'rake/packagetask'
|
10
|
+
require 'rake/gempackagetask'
|
11
|
+
|
12
|
+
begin
|
13
|
+
require 'spec/rake/spectask'
|
14
|
+
rescue LoadError
|
15
|
+
STDERR.puts "Please install rspec:"
|
16
|
+
STDERR.puts "sudo gem install rspec"
|
17
|
+
exit(1)
|
18
|
+
end
|
19
|
+
|
20
|
+
require File.join(File.dirname(__FILE__), 'lib/google/api_client', 'version')
|
21
|
+
|
22
|
+
PKG_DISPLAY_NAME = 'Google API Client'
|
23
|
+
PKG_NAME = PKG_DISPLAY_NAME.downcase.gsub(/\s/, '-')
|
24
|
+
PKG_VERSION = Google::APIClient::VERSION::STRING
|
25
|
+
PKG_FILE_NAME = "#{PKG_NAME}-#{PKG_VERSION}"
|
26
|
+
PKG_HOMEPAGE = 'http://code.google.com/p/google-api-ruby-client/'
|
27
|
+
|
28
|
+
RELEASE_NAME = "REL #{PKG_VERSION}"
|
29
|
+
|
30
|
+
PKG_SUMMARY = 'Package Summary'
|
31
|
+
PKG_DESCRIPTION = <<-TEXT
|
32
|
+
The Google API Ruby Client makes it trivial to discover and access supported
|
33
|
+
APIs.
|
34
|
+
TEXT
|
35
|
+
|
36
|
+
PKG_FILES = FileList[
|
37
|
+
'lib/**/*', 'spec/**/*', 'vendor/**/*',
|
38
|
+
'tasks/**/*', 'website/**/*',
|
39
|
+
'[A-Z]*', 'Rakefile'
|
40
|
+
].exclude(/database\.yml/).exclude(/[_\.]git$/)
|
41
|
+
|
42
|
+
RCOV_ENABLED = (RUBY_PLATFORM != 'java' && RUBY_VERSION =~ /^1\.8/)
|
43
|
+
if RCOV_ENABLED
|
44
|
+
task :default => 'spec:verify'
|
45
|
+
else
|
46
|
+
task :default => 'spec'
|
47
|
+
end
|
48
|
+
|
49
|
+
WINDOWS = (RUBY_PLATFORM =~ /mswin|win32|mingw|bccwin|cygwin/) rescue false
|
50
|
+
SUDO = WINDOWS ? '' : ('sudo' unless ENV['SUDOLESS'])
|
51
|
+
|
52
|
+
Dir['tasks/**/*.rake'].each { |rake| load rake }
|
data/bin/google-api
ADDED
@@ -0,0 +1,295 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
bin_dir = File.expand_path("..", __FILE__)
|
4
|
+
lib_dir = File.expand_path("../lib", bin_dir)
|
5
|
+
|
6
|
+
$LOAD_PATH.unshift(lib_dir)
|
7
|
+
$LOAD_PATH.uniq!
|
8
|
+
|
9
|
+
OAUTH_SERVER_PORT = 12736
|
10
|
+
|
11
|
+
require 'rubygems'
|
12
|
+
require 'optparse'
|
13
|
+
require 'google/api_client/version'
|
14
|
+
require 'google/api_client'
|
15
|
+
|
16
|
+
ARGV.unshift('--help') if ARGV.empty?
|
17
|
+
|
18
|
+
command = 'execute'
|
19
|
+
options = {}
|
20
|
+
OptionParser.new do |opts|
|
21
|
+
opts.banner =
|
22
|
+
"Usage: google-api <rpcname> [options] -- <parameters>\n" +
|
23
|
+
" or: google-api --oauth-login=<scope> [options]\n" +
|
24
|
+
" or: google-api --interactive=<service> [options]\n" +
|
25
|
+
" or: google-api --fuzz [options]"
|
26
|
+
|
27
|
+
opts.separator ""
|
28
|
+
|
29
|
+
opts.on(
|
30
|
+
"--oauth-login <scope>", String, "Authorize for the scope") do |s|
|
31
|
+
if command != 'execute'
|
32
|
+
STDERR.puts("Ambiguous command: #{command}")
|
33
|
+
exit(1)
|
34
|
+
end
|
35
|
+
command = 'oauth-login'
|
36
|
+
options[:scope] = s
|
37
|
+
end
|
38
|
+
opts.on(
|
39
|
+
"-s", "--service <name>", String, "Perform discovery on service") do |s|
|
40
|
+
options[:service_name] = s
|
41
|
+
end
|
42
|
+
opts.on(
|
43
|
+
"-i", "--interactive <name>", String, "Start interactive session") do |s|
|
44
|
+
if command != 'execute'
|
45
|
+
STDERR.puts("Ambiguous command: #{command}")
|
46
|
+
exit(1)
|
47
|
+
end
|
48
|
+
command = 'interactive'
|
49
|
+
options[:service_name] = s
|
50
|
+
end
|
51
|
+
opts.on(
|
52
|
+
"--service-version <id>", String, "Select service version") do |id|
|
53
|
+
options[:service_version] = id
|
54
|
+
end
|
55
|
+
opts.on(
|
56
|
+
"--content-type <format>", String, "Content-Type for request") do |f|
|
57
|
+
# Resolve content type shortcuts
|
58
|
+
case f
|
59
|
+
when 'json'
|
60
|
+
f = 'application/json'
|
61
|
+
when 'xml'
|
62
|
+
f = 'application/xml'
|
63
|
+
when 'atom'
|
64
|
+
f = 'application/atom+xml'
|
65
|
+
when 'rss'
|
66
|
+
f = 'application/rss+xml'
|
67
|
+
end
|
68
|
+
options[:content_type] = f
|
69
|
+
end
|
70
|
+
opts.on("--fuzz [rpcname]", String, "Fuzz an API or endpoint") do |rpcname|
|
71
|
+
if command != 'execute'
|
72
|
+
STDERR.puts("Ambiguous command: #{command}")
|
73
|
+
exit(1)
|
74
|
+
end
|
75
|
+
command = 'fuzz'
|
76
|
+
options[:fuzz] = rpcname
|
77
|
+
end
|
78
|
+
|
79
|
+
opts.on_tail("-v", "--verbose", "Run verbosely") do |v|
|
80
|
+
options[:verbose] = v
|
81
|
+
end
|
82
|
+
opts.on_tail("-h", "--help", "Show this message") do
|
83
|
+
puts opts
|
84
|
+
exit
|
85
|
+
end
|
86
|
+
opts.on_tail("--version", "Show version") do
|
87
|
+
puts "google-api-client (#{Google::APIClient::VERSION::STRING})"
|
88
|
+
exit
|
89
|
+
end
|
90
|
+
end.parse!
|
91
|
+
|
92
|
+
if command == 'oauth-login' # Guard to keep start-up time short
|
93
|
+
require 'webrick'
|
94
|
+
# Used for oauth login
|
95
|
+
class OAuthVerifierServlet < WEBrick::HTTPServlet::AbstractServlet
|
96
|
+
def do_GET(request, response)
|
97
|
+
$verifier ||= Addressable::URI.unencode_component(
|
98
|
+
request.request_uri.to_s[/\?.*oauth_verifier=([^&$]+)(&|$)/, 1]
|
99
|
+
)
|
100
|
+
response.status = WEBrick::HTTPStatus::RC_ACCEPTED
|
101
|
+
# This javascript will auto-close the tab after the verifier is obtained.
|
102
|
+
response.body = <<-HTML
|
103
|
+
<html>
|
104
|
+
<head>
|
105
|
+
<script>
|
106
|
+
function closeWindow() {
|
107
|
+
window.open('', '_self', '');
|
108
|
+
window.close();
|
109
|
+
}
|
110
|
+
setTimeout(closeWindow, 10);
|
111
|
+
</script>
|
112
|
+
</head>
|
113
|
+
<body>
|
114
|
+
You may close this window.
|
115
|
+
</body>
|
116
|
+
</html>
|
117
|
+
HTML
|
118
|
+
self.instance_variable_get('@server').stop
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
def oauth_login(options={})
|
124
|
+
require 'signet/oauth_1/client'
|
125
|
+
require 'launchy'
|
126
|
+
require 'yaml'
|
127
|
+
$verifier = nil
|
128
|
+
logger = WEBrick::Log.new('/dev/null') # TODO(bobaman): Cross-platform?
|
129
|
+
server = WEBrick::HTTPServer.new(
|
130
|
+
:Port => OAUTH_SERVER_PORT,
|
131
|
+
:Logger => logger,
|
132
|
+
:AccessLog => logger
|
133
|
+
)
|
134
|
+
trap("INT") { server.shutdown }
|
135
|
+
|
136
|
+
server.mount("/", OAuthVerifierServlet)
|
137
|
+
|
138
|
+
oauth_client = Signet::OAuth1::Client.new(
|
139
|
+
:temporary_credential_uri =>
|
140
|
+
'https://www.google.com/accounts/OAuthGetRequestToken',
|
141
|
+
:authorization_uri =>
|
142
|
+
'https://www.google.com/accounts/OAuthAuthorizeToken',
|
143
|
+
:token_credential_uri =>
|
144
|
+
'https://www.google.com/accounts/OAuthGetAccessToken',
|
145
|
+
:client_credential_key => 'anonymous',
|
146
|
+
:client_credential_secret => 'anonymous',
|
147
|
+
:callback => "http://localhost:#{OAUTH_SERVER_PORT}/"
|
148
|
+
)
|
149
|
+
scope = options[:scope]
|
150
|
+
# Special cases
|
151
|
+
case scope
|
152
|
+
when "https://www.googleapis.com/auth/buzz",
|
153
|
+
"https://www.googleapis.com/auth/buzz.readonly"
|
154
|
+
oauth_client.authorization_uri =
|
155
|
+
'https://www.google.com/buzz/api/auth/OAuthAuthorizeToken?' +
|
156
|
+
"domain=#{oauth_client.client_credential_key}&" +
|
157
|
+
"scope=#{scope}&" +
|
158
|
+
"xoauth_displayname=Google%20API%20Client"
|
159
|
+
end
|
160
|
+
oauth_client.fetch_temporary_credential!(:additional_parameters => {
|
161
|
+
:scope => scope,
|
162
|
+
:xoauth_displayname => 'Google API Client'
|
163
|
+
})
|
164
|
+
|
165
|
+
# Launch browser
|
166
|
+
Launchy::Browser.run(oauth_client.authorization_uri.to_s)
|
167
|
+
|
168
|
+
server.start
|
169
|
+
oauth_client.fetch_token_credential!(:verifier => $verifier)
|
170
|
+
config = {
|
171
|
+
"scope" => scope,
|
172
|
+
"client_credential_key" => oauth_client.client_credential_key,
|
173
|
+
"client_credential_secret" => oauth_client.client_credential_secret,
|
174
|
+
"token_credential_key" => oauth_client.token_credential_key,
|
175
|
+
"token_credential_secret" => oauth_client.token_credential_secret
|
176
|
+
}
|
177
|
+
config_file = File.expand_path('~/.google-api.yaml')
|
178
|
+
open(config_file, 'w') { |file| file.write(YAML.dump(config)) }
|
179
|
+
exit(0)
|
180
|
+
end
|
181
|
+
|
182
|
+
def execute(options={})
|
183
|
+
require 'signet/oauth_1/client'
|
184
|
+
require 'yaml'
|
185
|
+
config_file = File.expand_path('~/.google-api.yaml')
|
186
|
+
signed = File.exist?(config_file)
|
187
|
+
rpcname = ARGV.detect { |p| p =~ /^[a-z0-9_-]+\.[a-z0-9_\.-]+$/i }
|
188
|
+
if rpcname
|
189
|
+
ARGV.delete(rpcname)
|
190
|
+
else
|
191
|
+
STDERR.puts('Could not find rpcname.')
|
192
|
+
exit(1)
|
193
|
+
end
|
194
|
+
service_name = options[:service_name] || rpcname[/^([^\.]+)\./, 1]
|
195
|
+
client = Google::APIClient.new(
|
196
|
+
:service => service_name,
|
197
|
+
:authorization => :oauth_1
|
198
|
+
)
|
199
|
+
if signed
|
200
|
+
if !client.authorization.kind_of?(Signet::OAuth1::Client)
|
201
|
+
STDERR.puts(
|
202
|
+
"Unexpected authorization mechanism: #{client.authorization.class}"
|
203
|
+
)
|
204
|
+
exit(1)
|
205
|
+
end
|
206
|
+
config = open(config_file, 'r') { |file| YAML.load(file.read) }
|
207
|
+
client.authorization.client_credential_key =
|
208
|
+
config["client_credential_key"]
|
209
|
+
client.authorization.client_credential_secret =
|
210
|
+
config["client_credential_secret"]
|
211
|
+
client.authorization.token_credential_key =
|
212
|
+
config["token_credential_key"]
|
213
|
+
client.authorization.token_credential_secret =
|
214
|
+
config["token_credential_secret"]
|
215
|
+
end
|
216
|
+
service_version =
|
217
|
+
options[:service_version] ||
|
218
|
+
client.latest_service_version(service_name).version
|
219
|
+
service = client.discovered_service(service_name, service_version)
|
220
|
+
method = service.to_h[rpcname]
|
221
|
+
if !method
|
222
|
+
STDERR.puts(
|
223
|
+
"Method #{rpcname} does not exist for " +
|
224
|
+
"#{service_name}-#{service_version}."
|
225
|
+
)
|
226
|
+
exit(1)
|
227
|
+
end
|
228
|
+
parameters = ARGV.inject({}) do |accu, pair|
|
229
|
+
name, value = pair.split('=', 2)
|
230
|
+
accu[name] = value
|
231
|
+
accu
|
232
|
+
end
|
233
|
+
request_body = ''
|
234
|
+
input_streams, _, _ = IO.select([STDIN], [], [], 0)
|
235
|
+
request_body = STDIN.read || '' if input_streams
|
236
|
+
headers = []
|
237
|
+
if options[:content_type]
|
238
|
+
headers << ['Content-Type', options[:content_type]]
|
239
|
+
elsif request_body
|
240
|
+
# Default to JSON
|
241
|
+
headers << ['Content-Type', 'application/json']
|
242
|
+
end
|
243
|
+
response = client.execute(
|
244
|
+
method, parameters, request_body, headers, {:signed => signed}
|
245
|
+
)
|
246
|
+
status, headers, body = response
|
247
|
+
puts body
|
248
|
+
exit(0)
|
249
|
+
end
|
250
|
+
|
251
|
+
def interactive(options={})
|
252
|
+
require 'signet/oauth_1/client'
|
253
|
+
require 'yaml'
|
254
|
+
config_file = File.expand_path('~/.google-api.yaml')
|
255
|
+
signed = File.exist?(config_file)
|
256
|
+
|
257
|
+
$client = Google::APIClient.new(
|
258
|
+
:service => options[:service_name],
|
259
|
+
:authorization => (signed ? :oauth_1 : nil)
|
260
|
+
)
|
261
|
+
|
262
|
+
if signed
|
263
|
+
if $client.authorization &&
|
264
|
+
!$client.authorization.kind_of?(Signet::OAuth1::Client)
|
265
|
+
STDERR.puts(
|
266
|
+
"Unexpected authorization mechanism: #{$client.authorization.class}"
|
267
|
+
)
|
268
|
+
exit(1)
|
269
|
+
end
|
270
|
+
config = open(config_file, 'r') { |file| YAML.load(file.read) }
|
271
|
+
$client.authorization.client_credential_key =
|
272
|
+
config["client_credential_key"]
|
273
|
+
$client.authorization.client_credential_secret =
|
274
|
+
config["client_credential_secret"]
|
275
|
+
$client.authorization.token_credential_key =
|
276
|
+
config["token_credential_key"]
|
277
|
+
$client.authorization.token_credential_secret =
|
278
|
+
config["token_credential_secret"]
|
279
|
+
end
|
280
|
+
|
281
|
+
require 'irb'
|
282
|
+
IRB.start(__FILE__)
|
283
|
+
end
|
284
|
+
|
285
|
+
def fuzz(options={})
|
286
|
+
STDERR.puts('API fuzzing not yet supported.')
|
287
|
+
if rpcname
|
288
|
+
# Fuzz just one method
|
289
|
+
else
|
290
|
+
# Fuzz the entire API
|
291
|
+
end
|
292
|
+
exit(1)
|
293
|
+
end
|
294
|
+
|
295
|
+
self.send(command.gsub(/-/, "_").to_sym, options)
|