My favorites | Sign in
Project Logo
                
Search
for
Updated Nov 25, 2007 by pelleb
AccessToken  
How the AccessToken api works

Introduction

The AccessToken is used for accessing Web Services using the OAuth protocol.

See Spec

This is only used from the ConsumerService side.

AccessToken lifecycle

  1. ConsumerService Request an AccessToken from the ServiceProvider using an authorized RequestToken
  2. ConsumerService ServiceProvider's web service using the AccessToken's http request wrapper methods

Contains

  • token - the actual token string
  • secret - a secret string used for signing requests

Creation

You ask the RequestToken to exchange it self for an AccessToken from the ServiceProvider:

@access_token=@request_token.get_access_token

Accessing REST webservices

It wraps the ruby http/s object with simple methods for each http method:

# GET /people.xml
@response=@access_token.get('/people.xml')

# GET /people/1.xml
@response=@access_token.get('/people/1.xml')

# POST /people
@response=@access_token.post('/people',"person[name]=Bob")

# POST /people with xml
@response=@access_token.post('/people',@person.to_xml,{'Content-Type'=>'application/xml')

# PUT /people/1
@response=@access_token.post('/people/1',"person[name]=Bob%20Smith")

# PUT /people/1 with xml
@response=@access_token.put('/people/1',@person.to_xml,{'Content-Type'=>'application/xml')

# DELETE /people/1
@response=@access_token.delete('/people/1')

Comment by humbroll, Apr 01, 2008

there's some errata. {'Content-Type'=>'application/xml' => {'Content-Type'=>'application/xml'}

thanks for great document.

Comment by avbentem, Jun 22, 2008

Using a long living AccessToken

If the provider does not invalidate the access token until explicitly requested to do so, then one can keep the string values to use at a later time:

# Initialisation based on string values:
consumer_key = 'AVff2raXvhMUxFnif06g'
consumer_secret = 'u0zg77R1bQqbzutAusJYmTxqeUpWVt7U2TjWlzbVZkA'
access_token = 'R1bQqbzYm0zg77tAusJzbVZkAVt7U2T'
access_token_secret = 'sVbVZkAt7U2TjWlJYmTxqR1bQqbzutAuWzeUpu0zg77'

@consumer = OAuth::Consumer.new(consumer_key, consumer_secret, {:site=>'http://my.site'})


@accesstoken = OAuth::AccessToken.new(@consumer, access_token, access_token_secret)

Sending specific HTTP headers

To send specific HTTP headers such as User-Agent or Accept:

@response = @access_token.get('/people.xml', {'User-Agent'=>'my user agent'})

Sign in to add a comment
Hosted by Google Code