Permalink
Browse files

Method for making OAuth API requests and support

getting user id of user who authorized OAuth.
  • Loading branch information...
1 parent d59993d commit 35bec6023c7a64f84de11ff4bfe7a7ef29e7763c @thegreatape committed Dec 10, 2012
View
@@ -118,6 +118,17 @@ shelf.end # end index of this page of paginated results
shelf.total # total number of books on this shelf
```
+### User Id
+
+Get the user id of the user who authorized via OAuth:
+
+```ruby
+client = Goodreads::Client.new(:api_key => 'YOUR_KEY', :oauth_token => token)
+client.user_id # id of user who authorized via OAuth
+```
+
+`token` is an instance of `OAuth::AccessToken`. See the [Goodreads documentation](http://www.goodreads.com/api/oauth_example) for examples of how to correct create one.
+
## Contributions
Feel free to contribute any patches or new features.
View
@@ -19,9 +19,10 @@ Gem::Specification.new do |s|
s.add_runtime_dependency 'hashie', '~> 1.0'
s.add_runtime_dependency 'activesupport', '~> 3'
s.add_runtime_dependency 'i18n', '~> 0.5'
+ s.add_runtime_dependency 'oauth', '~> 0.4'
s.files = `git ls-files`.split("\n")
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
s.executables = `git ls-files -- bin/*`.split("\n").map{|f| File.basename(f)}
s.require_paths = ["lib"]
-end
+end
@@ -4,6 +4,7 @@
require 'goodreads/client/authors'
require 'goodreads/client/users'
require 'goodreads/client/shelves'
+require 'goodreads/client/authorized'
module Goodreads
class Client
@@ -13,13 +14,15 @@ class Client
include Goodreads::Authors
include Goodreads::Users
include Goodreads::Shelves
+ include Goodreads::Authorized
- attr_reader :api_key, :api_secret
+ attr_reader :api_key, :api_secret, :oauth_token
# Initialize a Goodreads::Client instance
#
- # options[:api_key] - Account API key
- # options[:api_secret] - Account API secret
+ # options[:api_key] - Account API key
+ # options[:api_secret] - Account API secret
+ # options[:oauth_token] - OAuth token (optional, required for some calls)
#
def initialize(options={})
unless options.kind_of?(Hash)
@@ -28,6 +31,7 @@ def initialize(options={})
@api_key = options[:api_key]
@api_secret = options[:api_secret]
+ @oauth_token = options[:oauth_token]
end
end
-end
+end
@@ -0,0 +1,9 @@
+module Goodreads
+ module Authorized
+
+ def user_id
+ oauth_request('/api/auth_user')['user']['id']
+ end
+
+ end
+end
@@ -34,10 +34,29 @@ def request(path, params={})
raise Goodreads::NotFound
end
end
-
- hash = Hash.from_xml(resp)['GoodreadsResponse']
+
+ parse(resp)
+ end
+
+ def oauth_request(path, params=nil)
+ raise 'OAuth access token required!' unless @oauth_token
+ path = "#{path}?#{params.map{|k,v|"#{k}=#{v}"}.join('&')}" if params
+ resp = @oauth_token.get(path)
+ case resp
+ when Net::HTTPUnauthorized
+ raise Goodreads::Unauthorized
+ when Net::HTTPNotFound
+ raise Goodreads::NotFound
+ end
+
+ parse(resp)
+ end
+
+ def parse(resp)
+ hash = Hash.from_xml(resp.body)['GoodreadsResponse']
hash.delete('Request')
hash
end
+
end
end
View
@@ -1,4 +1,5 @@
require 'spec_helper'
+require 'oauth'
describe 'Client' do
before :each do
@@ -167,4 +168,15 @@
@shelf.total.should == 0
@shelf.books.length.should == 0
end
+
+ it "should return the user id of the user who authorized OAuth" do
+ stub_request(:get, "http://www.goodreads.com/api/auth_user")
+ .to_return(:status => 200, :body => fixture('oauth_response.xml'), :headers => {})
+
+ consumer = OAuth::Consumer.new('API_KEY', 'SECRET_KEY', :site => 'http://www.goodreads.com')
+ oauth_token = OAuth::AccessToken.new(consumer, 'ACCESS_TOKEN', 'ACCESS_SECRET')
+
+ @client = Goodreads::Client.new(:api_key => 'SECRET_KEY', :oauth_token => oauth_token)
+ @client.user_id.should == '2003928'
+ end
end
@@ -0,0 +1,12 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<GoodreadsResponse>
+ <Request>
+ <authentication>true</authentication>
+ <key><![CDATA[SECRET_KEY]]></key>
+ <method><![CDATA[api_auth_user]]></method>
+</Request>
+ <user id="2003928">
+ <name>Thomas Mayfield</name>
+ <link><![CDATA[http://www.goodreads.com/user/show/2003928-thomas?utm_medium=api]]></link>
+ </user>
+</GoodreadsResponse>

0 comments on commit 35bec60

Please sign in to comment.