Class: Agree2::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/agree2/client.rb

Overview

If you haven’t done so already register your application at agree2.com/client_applications

The full authorization process works like this over 2 controller actions (This example is in Rails):

 def request_token
   @client = Agree2::Client.new "key","secret"
   @request_token = @client.get_request_token

   # Store the token in a model in your applications mapping it to your user
   Agree2RequestToken.create :user=>current_user,:token=>@request_token.token,:secret=>@request_token.secret
   redirect_to @request_token.authorize_url
 end

 # The user authorizes the token on the Agree2 web site and is redirected to the authorize_url you setup when you registered your application at:
 # https://agree2.com/client_applications
 def authorize
   @client = Agree2::Client.new "key","secret"

   # Load the request token from your Agree2RequestToken through your user model
   @request_token = current_user.agree2_request_token.request_token

   # Exchange the authorized request token for a more permanent User token on the Agree2 site
   @user_client=@client.user_from_request_token(@request_token)

   # Store the Agree2 user data in your own model
   @agree2_user=Agree2User.create :user=>current_user,:token=>@user_client.token,:secret=>@user_client.secret
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, secret) ⇒ Client

Initialize the Agree2 Client

Required Fields

  • key - The Consumer Key

  • secret - The Consumer Secret

To get these register your application at: agree2.com/client_applications



44
45
46
# File 'lib/agree2/client.rb', line 44

def initialize(key,secret)
  @consumer=OAuth::Consumer.new(key,secret,{:site=>AGREE2_URL,:ca_file=>File.join(File.dirname(__FILE__), '..','..','certs','sf_bundle.crt')})
end

Instance Attribute Details

#consumerObject

Returns the value of attribute consumer.



34
35
36
# File 'lib/agree2/client.rb', line 34

def consumer
  @consumer
end

Instance Method Details

#get_request_token(request_options = {}) ⇒ Object

Start the process of authorizing a token for an Agree2 user.

Example:

@request_token = @client.get_request_token
redirect_to @request_token.authorize_url

If you want to pass a callback_url you should do so here.

@request_token = @client.get_request_token :oauth_callback => "http://example.com/cb"
 redirect_to @request_token.authorize_url


71
72
73
# File 'lib/agree2/client.rb', line 71

def get_request_token(request_options = {})
  consumer.get_request_token request_options
end

#keyObject

Returns your consumer key



100
101
102
# File 'lib/agree2/client.rb', line 100

def key
  @consumer.key
end

#secretObject

Returns your consumer secret



105
106
107
# File 'lib/agree2/client.rb', line 105

def secret
  @consumer.secret
end

#user(token, token_secret) ⇒ Object

initialize a new user object with the given token and secret. The user object is what you use to do most of the work. Use this method when you need to create a user object from a token and secret you have stored in your applications database.

Required Fields

  • token - This is the authorized Token

  • secret - This is the authorized Token Secret



56
57
58
# File 'lib/agree2/client.rb', line 56

def user(token,token_secret)
  User.new(self,token,token_secret)
end

#user_from_request_token(request_token, oauth_verifier) ⇒ Object

Exchange an Authorized RequestToken for a working user object

Required Field

  • request_token The Request token created using get_request_token and authorized on Agree2 by your user

  • oauth_verifier The oauth_verifier passed to your callback url

Example:

@request_token = ClientRequestToken.find_by_token params[:oauth_token]
@user_client = @client.user_from_request_token(@request_token,params[:oauth_verifier])


88
89
90
91
92
93
94
95
96
97
# File 'lib/agree2/client.rb', line 88

def user_from_request_token(request_token,oauth_verifier)
  access_token=request_token.get_access_token(:oauth_verifier=>oauth_verifier)
  user(access_token.token,access_token.secret)
rescue Net::HTTPServerException=>e
  if e.response.code=='401'
    raise Agree2Exception,"The user has not authorized this request token",caller
  else
    raise
  end
end