1 # Copyright 2010 Google Inc.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 require "addressable/uri"
18 module Google #:nodoc:
19 class APIClient #:nodoc:
22 # The default OAuth 1.0a configuration values. These may be overrided
23 # simply by passing in the same key to the constructor.
26 'https://www.google.com/accounts/OAuthGetRequestToken',
28 'https://www.google.com/accounts/OAuthAuthorizeToken',
30 'https://www.google.com/accounts/OAuthGetAccessToken',
32 :callback => OAuth::OUT_OF_BAND,
34 :consumer_key => "anonymous",
35 :consumer_secret => "anonymous"
39 # A set of default configuration values specific to each service. These
40 # may be overrided simply by passing in the same key to the constructor.
44 'https://www.google.com/buzz/api/auth/OAuthAuthorizeToken',
45 :scopes => ["https://www.googleapis.com/auth/buzz"]
50 # Creates a new OAuth 1.0a handler. This object obtains the tokens from
51 # the provider and handles signing any requests to the API.
53 # @param [Hash] options
54 # The configuration options.
55 # <code>:service</code>::
56 # The name of the service.
57 # <code>:request_token_uri</code>::
58 # The OAuth endpoint for obtaining a request token.
59 # <code>:authorization_uri</code>::
60 # The OAuth endpoint for obtaining user permission.
61 # <code>:access_token_uri</code>::
62 # The OAuth endpoint for obtaining an access token.
63 # <code>:scopes</code>::
64 # An <code>Array</code> of scopes that define the access being
65 # requested to the API.
66 # <code>:callback</code>::
67 # The URI the user will be redirected to if access is granted to the
68 # API. For development purposes, the special value
69 # <code>OAuth::OUT_OF_BAND</code> may also be used.
70 # <code>:display_name</code>::
71 # A human-readable service name to present to the user when they
72 # visit the <code>:authorization_uri</code>.
73 # <code>:consumer_key</code>::
74 # The consumer key you registered with the Google Accounts API.
75 # <code>:consumer_secret</code>::
76 # The consumer secret issued to you when you registered with the
77 # Google Accounts API.
79 # @return [Google::APIClient::OAuth1] The OAuth 1.0a handler.
80 def initialize(options={})
81 if options[:service] && SERVICE_DEFAULTS[options[:service]]
82 @options = DEFAULTS.merge(SERVICE_DEFAULTS[options[:service]])
84 @options = DEFAULTS.clone
86 @options.merge!(options)
87 @options[:request_token_uri] =
88 Addressable::URI.parse(@options[:request_token_uri])
89 @options[:authorization_uri] =
90 Addressable::URI.parse(@options[:authorization_uri])
91 @options[:access_token_uri] =
92 Addressable::URI.parse(@options[:access_token_uri])
93 if (@options[:request_token_uri].site !=
94 @options[:authorization_uri].site) ||
95 (@options[:request_token_uri].site !=
96 @options[:authorization_uri].site)
97 raise ArgumentError, "All OAuth endpoints must be on the same site."
99 @oauth_consumer = ::OAuth::Consumer.new(
100 @options[:consumer_key], @options[:consumer_secret], {
101 # This is an extremely unfortunate way to configure the consumer,
102 # but not worth forking or patching to resolve. Yet.
103 :site => @options[:request_token_uri].site,
105 :http_method => :post,
106 :request_token_path => @options[:request_token_uri].request_uri,
107 :access_token_path => @options[:access_token_uri].request_uri,
108 :authorize_path => @options[:authorization_uri].request_uri
114 # Returns the configuration of the handler. Configuration options that
115 # are not recognized by the handler are ignored.
117 # @return [Hash] The configuration options.
123 # Returns the current request token. Obtains a new request token if
124 # one hasn't already been obtained.
126 # @return [OAuth::RequestToken] The request token.
129 :oauth_callback => @options[:callback]
132 :scope => @options[:scopes].join(" ")
134 if @options[:display_name]
135 app_parameters[:xoauth_displayname] = @options[:display_name]
137 return @request_token ||= @oauth_consumer.get_request_token(
144 # Sets the request token for the handler.
146 # @param [OAuth::RequestToken] new_request_token The request token.
147 def request_token=(new_request_token)
148 if new_request_token.kind_of?(OAuth::RequestToken)
149 @request_token = new_request_token
152 "Expected OAuth::RequestToken, got #{new_request_token.class}."
157 # Returns the current access token. Obtains a new access token if
158 # one hasn't already been obtained. An request token must have already
159 # been obtained and authorized or this method will fail.
161 # @return [OAuth::AccessToken] The access token.
163 return @access_token ||=
164 @oauth_consumer.get_access_token(self.request_token)
168 # Sets the access token for the handler.
170 # @param [OAuth::AccessToken] new_access_token The access token.
171 def access_token=(new_access_token)
172 if new_access_token.kind_of?(OAuth::AccessToken)
173 @access_token = new_access_token
176 "Expected OAuth::AccessToken, got #{new_access_token.class}."
181 # Returns the list of scopes for the handler.
183 # @return [Array] An <code>Array</code> of access scopes.
185 return @options[:scopes]
189 # Returns the callback for the handler.
191 # @return [String] The OAuth 1.0a callback for the consumer.
193 return @options[:callback]
197 # Returns a human-readable service name to present to the user when they
198 # visit the <code>:authorization_uri</code>.
200 # @return [String] The display name for the consumer.
202 return @options[:display_name]
206 # Returns the consumer key.
209 # The consumer key you registered with the Google Accounts API.
211 return @oauth_consumer.key
215 # Returns the consumer key.
218 # The consumer secret issued to you when you registered with the
219 # Google Accounts API.
221 return @oauth_consumer.secret
225 # Returns the request token URI.
228 # The OAuth endpoint for obtaining a request token.
229 def request_token_uri
230 return @oauth_consumer.request_token_url
234 # Returns the authorization endpoint URI. This URI is used to construct
235 # the {#authorization_uri}.
238 # The OAuth endpoint for obtaining user permission.
239 def authorization_endpoint_uri
240 return @oauth_consumer.authorize_url
244 # Builds the authorization URI that the user will be redirected to.
245 # Note that this value is derived from the
246 # {#authorization_endpoint_uri}.
248 # @param [Hash] parameters
249 # The extra URI query parameters appended to the
250 # {#authorization_endpoint_uri}.
253 # The URI to redirect the user to to obtain permission.
254 def authorization_uri(parameters={})
255 return self.request_token.authorize_url(parameters)
259 # Returns the access token URI.
262 # The OAuth endpoint for obtaining an access token.
264 return @oauth_consumer.access_token_url