Permalink
Browse files

spiked out oauth 1 support, with api key, fixes for 1.8.*

  • Loading branch information...
1 parent 6d5e501 commit b9b7d8b6054c32dc6026a33063a51be036bb91a7 Tony Pitale committed Jan 20, 2012
View
5 Gemfile
@@ -2,3 +2,8 @@ source "http://rubygems.org"
# Specify your gem's dependencies in legato.gemspec
gemspec
+
+group :development do
+ gem 'oauth2'
+ gem 'oauth'
+end
View
38 Rakefile
@@ -1,5 +1,6 @@
require "bundler/gem_tasks"
+require 'oauth'
require 'oauth2'
# get oauth2 via device code, unimplemented as of now
@@ -8,7 +9,7 @@ require 'oauth2'
# curl -d "client_id={{client_id}}&client_secret={{client_secret}}&code=4/8O1xUKWzOdJESG7ednnulZPsbyNt&grant_type=http://oauth.net/grant_type/device/1.0" https://accounts.google.com/o/oauth2/token
# { "access_token" : "ERspXATXoblahblahblah", "token_type" : "Bearer", "expires_in" : 3600, "refresh_token" : "1/balhaajsdfklasfdjs;df" }
-namespace :oauth do
+namespace :oauth2 do
def client
# This is my test client account for Legato.
OAuth2::Client.new('779170787975.apps.googleusercontent.com', 'mbCISoZiSwyVQIDEbLj4EeEc', {
@@ -35,3 +36,38 @@ namespace :oauth do
puts access_token.token
end
end
+
+namespace :oauth do
+ def consumer
+ OAuth::Consumer.new('779170787975.apps.googleusercontent.com', 'mbCISoZiSwyVQIDEbLj4EeEc', {
+ :site => "https://www.google.com",
+ :request_token_path => "/accounts/OAuthGetRequestToken",
+ :access_token_path => "/accounts/OAuthGetAccessToken",
+ :authorize_path => "/accounts/OAuthAuthorizeToken"
+ })
+ end
+
+ def request_token
+ @request_token ||= consumer.get_request_token({}, {:scope => "https://www.googleapis.com/auth/analytics.readonly"})
+ end
+
+ def auth_url
+ request_token.authorize_url
+ end
+
+ def access_token
+ @access_token ||= begin
+ print 'OAuth Code: '
+ code = $stdin.gets.strip
+ request_token.get_access_token(:oauth_verifier => code)
+ end
+ end
+
+ desc "Get a new OAuth Token"
+ task :token do
+ `open "#{auth_url}"`
+
+ puts "Token: #{access_token.token}"
+ puts "Secret: #{access_token.secret}"
+ end
+end
View
8 lib/legato/management/finder.rb
@@ -6,6 +6,14 @@ def base_uri
end
def all(user, path=default_path)
+ uri = if user.api_key
+ # oauth + api_key
+ base_uri + path + "?key=#{user.api_key}"
+ else
+ # oauth 2
+ base_uri + path
+ end
+
json = user.access_token.get(base_uri + path).body
MultiJson.decode(json)['items'].map {|item| new(item, user)}
end
View
5 lib/legato/query.rb
@@ -192,6 +192,11 @@ def to_params
params.reject {|k,v| v.nil? || v.to_s.strip.length == 0}
end
+ def to_query_string
+ list = to_params.map {|k,v| [k,v].join("=")}
+ "?#{list.join("&")}"
+ end
+
private
def request_for_query
profile.user.request(self)
View
15 lib/legato/user.rb
@@ -1,16 +1,25 @@
module Legato
class User
- attr_accessor :access_token
+ attr_accessor :access_token, :api_key
- def initialize(token)
+ def initialize(token, api_key = nil)
self.access_token = token
+ self.api_key = api_key
end
URL = "https://www.googleapis.com/analytics/v3/data/ga"
def request(query)
begin
- Response.new(access_token.get(URL, :params => query.to_params))
+ raw_response = if api_key
+ # oauth 1 + api key
+ access_token.get(URL + query.to_query_string + "&key=#{api_key}")
+ else
+ # oauth 2
+ access_token.get(URL, :params => query.to_params)
+ end
+
+ Response.new(raw_response)
rescue => e
p e.code
raise e
View
2 spec/lib/legato/management/account_spec.rb
@@ -9,7 +9,7 @@ def self.subject_class_name
it_behaves_like "a management finder"
it 'creates a new account instance from a hash of attributes' do
- user = stub
+ user = stub(:api_key => nil)
account = Legato::Management::Account.new({"id" => 12345, "name" => "Account 1"}, user)
account.user.should == user
account.id.should == 12345
View
2 spec/support/examples/management_finder.rb
@@ -3,7 +3,7 @@
MultiJson.stubs(:decode).returns({'items' => ['item1', 'item2']})
response = stub(:body => 'some json')
access_token = stub(:get => response)
- user = stub(:access_token => access_token)
+ user = stub(:access_token => access_token, :api_key => nil)
described_class.stubs(:new).returns('thing1', 'thing2')
described_class.all(user).should == ['thing1', 'thing2']

0 comments on commit b9b7d8b

Please sign in to comment.