Skip to content

Commit

Permalink
Add Google subclass for accessing Gmail via OAuth.
Browse files Browse the repository at this point in the history
  • Loading branch information
lfittl committed Jan 31, 2009
1 parent 6967956 commit 09c3c07
Showing 1 changed file with 66 additions and 0 deletions.
66 changes: 66 additions & 0 deletions lib/contacts/google_oauth.rb
@@ -0,0 +1,66 @@
require 'oauth'

# An extension to the standard OAuth library so we can nicely use Google APIs
module GoogleOAuth
class RequestToken < OAuth::RequestToken
def authorize_url(params={})
params.merge! :oauth_token => token
params = params.map { |k,v| "%s=%s" % [CGI.escape(k.to_s), CGI.escape(v)] }
consumer.authorize_url + "?" + params.join("&")
end
end

class Consumer < OAuth::Consumer
def initialize(consumer_key, consumer_secret)
super(consumer_key,
consumer_secret,
{:site => "https://www.google.com",
:request_token_path => "/accounts/OAuthGetRequestToken",
:access_token_path => "/accounts/OAuthGetAccessToken",
:authorize_path => "/accounts/OAuthAuthorizeToken"})
end

def get_request_token(params={})
params_str = params.map { |k,v| "%s=%s" % [CGI.escape(k.to_s), CGI.escape(v)] }.join("&")
uri = URI.parse(request_token_url? ? request_token_url : request_token_path)
if !uri.query || uri.query == ''
uri.query = params_str
else
uri.query = uri.query + "&" + params_str
end

response=token_request(http_method, uri.to_s, nil, {})
GoogleOAuth::RequestToken.new(self, response[:oauth_token], response[:oauth_token_secret])
end
end
end

module Contacts
class GoogleOAuth < Google
def initialize(consumer_key, consumer_secret, user_id = 'default')
@consumer = ::GoogleOAuth::Consumer.new(consumer_key, consumer_secret)
@request_token = @consumer.get_request_token :scope => "https://www.google.com/m8/feeds/"
@projection = 'thin'
@user = user_id.to_s
end

# Available parameters:
# - hd: Google Apps domain that should be requested (default nil)
# - oauth_callback: The URL that the user should be redirected to when he successfully authorized us.
def authentication_url(params={})
@request_token.authorize_url params
end

def access_token
return @access_token if @access_token
@access_token = @request_token.get_access_token
end

def get(params)
path = FeedsPath + CGI.escape(@user)
google_params = translate_parameters(params)
query = self.class.query_string(google_params)
access_token.get("#{path}/#{@projection}?#{query}")
end
end
end

0 comments on commit 09c3c07

Please sign in to comment.