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:
21 # An OAuth 1.0a handler.
24 # The default OAuth 1.0a configuration values. These may be overrided
25 # simply by passing in the same key to the constructor.
28 'https://www.google.com/accounts/OAuthGetRequestToken',
30 'https://www.google.com/accounts/OAuthAuthorizeToken',
32 'https://www.google.com/accounts/OAuthGetAccessToken',
34 :callback => OAuth::OUT_OF_BAND,
36 :consumer_key => "anonymous",
37 :consumer_secret => "anonymous"
41 # A set of default configuration values specific to each service. These
42 # may be overrided simply by passing in the same key to the constructor.
46 'https://www.google.com/buzz/api/auth/OAuthAuthorizeToken',
47 :scopes => ["https://www.googleapis.com/auth/buzz"]
52 # Creates a new OAuth 1.0a handler. This object obtains the tokens from
53 # the provider and handles signing any requests to the API.
55 # @param [Hash] options
56 # The configuration options.
57 # <code>:service</code>::
58 # The name of the service.
59 # <code>:request_token_uri</code>::
60 # The OAuth endpoint for obtaining a request token.
61 # <code>:authorization_uri</code>::
62 # The OAuth endpoint for obtaining user permission.
63 # <code>:access_token_uri</code>::
64 # The OAuth endpoint for obtaining an access token.
65 # <code>:scopes</code>::
66 # An <code>Array</code> of scopes that define the access being
67 # requested to the API.
68 # <code>:callback</code>::
69 # The URI the user will be redirected to if access is granted to the
70 # API. For development purposes, the special value
71 # <code>OAuth::OUT_OF_BAND</code> may also be used.
72 # <code>:display_name</code>::
73 # A human-readable service name to present to the user when they
74 # visit the <code>:authorization_uri</code>.
75 # <code>:consumer_key</code>::
76 # The consumer key you registered with the Google Accounts API.
77 # <code>:consumer_secret</code>::
78 # The consumer secret issued to you when you registered with the
79 # Google Accounts API.
81 # @return [Google::APIClient::OAuth1] The OAuth 1.0a handler.
82 def initialize(options={})
83 if options[:service] && SERVICE_DEFAULTS[options[:service]]
84 @options = DEFAULTS.merge(SERVICE_DEFAULTS[options[:service]])
86 @options = DEFAULTS.clone
88 @options.merge!(options)
89 @options[:request_token_uri] =
90 Addressable::URI.parse(@options[:request_token_uri])
91 @options[:authorization_uri] =
92 Addressable::URI.parse(@options[:authorization_uri])
93 @options[:access_token_uri] =
94 Addressable::URI.parse(@options[:access_token_uri])
95 if (@options[:request_token_uri].site !=
96 @options[:authorization_uri].site) ||
97 (@options[:request_token_uri].site !=
98 @options[:authorization_uri].site)
99 raise ArgumentError, "All OAuth endpoints must be on the same site."
101 @oauth_consumer = ::OAuth::Consumer.new(
102 @options[:consumer_key], @options[:consumer_secret], {
103 # This is an extremely unfortunate way to configure the consumer,
104 # but not worth forking or patching to resolve. Yet.
105 :site => @options[:request_token_uri].site,
107 :http_method => :post,
108 :request_token_path => @options[:request_token_uri].request_uri,
109 :access_token_path => @options[:access_token_uri].request_uri,
110 :authorize_path => @options[:authorization_uri].request_uri
116 # Returns the configuration of the handler. Configuration options that
117 # are not recognized by the handler are ignored.
119 # @return [Hash] The configuration options.
125 # Returns the current request token. Obtains a new request token if
126 # one hasn't already been obtained.
128 # @return [OAuth::RequestToken] The request token.
131 :oauth_callback => @options[:callback]
134 :scope => @options[:scopes].join(" ")
136 if @options[:display_name]
137 app_parameters[:xoauth_displayname] = @options[:display_name]
139 return @request_token ||= @oauth_consumer.get_request_token(
146 # Sets the request token for the handler.
148 # @param [OAuth::RequestToken] new_request_token The request token.
149 def request_token=(new_request_token)
150 if new_request_token.kind_of?(OAuth::RequestToken)
151 @request_token = new_request_token
154 "Expected OAuth::RequestToken, got #{new_request_token.class}."
159 # Returns the current access token. Obtains a new access token if
160 # one hasn't already been obtained. An request token must have already
161 # been obtained and authorized or this method will fail.
163 # @return [OAuth::AccessToken] The access token.
165 return @access_token ||=
166 @oauth_consumer.get_access_token(self.request_token)
170 # Sets the access token for the handler.
172 # @param [OAuth::AccessToken] new_access_token The access token.
173 def access_token=(new_access_token)
174 if new_access_token.kind_of?(OAuth::AccessToken)
175 @access_token = new_access_token
178 "Expected OAuth::AccessToken, got #{new_access_token.class}."
183 # Returns the list of scopes for the handler.
185 # @return [Array] An <code>Array</code> of access scopes.
187 return @options[:scopes]
191 # Returns the callback for the handler.
193 # @return [String] The OAuth 1.0a callback for the consumer.
195 return @options[:callback]
199 # Returns a human-readable service name to present to the user when they
200 # visit the <code>:authorization_uri</code>.
202 # @return [String] The display name for the consumer.
204 return @options[:display_name]
208 # Returns the consumer key.
211 # The consumer key you registered with the Google Accounts API.
213 return @oauth_consumer.key
217 # Returns the consumer key.
220 # The consumer secret issued to you when you registered with the
221 # Google Accounts API.
223 return @oauth_consumer.secret
227 # Returns the request token URI.
230 # The OAuth endpoint for obtaining a request token.
231 def request_token_uri
232 return @oauth_consumer.request_token_url
236 # Returns the authorization endpoint URI. This URI is used to construct
237 # the {#authorization_uri}.
240 # The OAuth endpoint for obtaining user permission.
241 def authorization_endpoint_uri
242 return @oauth_consumer.authorize_url
246 # Builds the authorization URI that the user will be redirected to.
247 # Note that this value is derived from the
248 # {#authorization_endpoint_uri}.
250 # @param [Hash] parameters
251 # The extra URI query parameters appended to the
252 # {#authorization_endpoint_uri}.
255 # The URI to redirect the user to to obtain permission.
256 def authorization_uri(parameters={})
257 return self.request_token.authorize_url(parameters)
261 # Returns the access token URI.
264 # The OAuth endpoint for obtaining an access token.
266 return @oauth_consumer.access_token_url