Code Example

We have provided example code written in Python that uses Leah Culver's OAuth Python Library. Any OAuth Consumer implementation should be at least similar to our example Python script and you should be able to model your own implementation based on it. For a complete reference of how to implement an OAuth Consumer and additional example code in a variety of languages, visit the official OAuth website.

Traditional OAuth

Leah Culver's Python Library contains some example code detailing the traditional (3-legged) OAuth implementation. There are also a variety of examples written in many different languages accessible on the OAuth official website.

Two-Legged OAuth (No User Authorization)

################################################
#
# Example snippet that makes use of OAuth
# 2-legged authorization to retrieve the
# last NUM_DAYS days strands.com workouts 
# from DATE
#
# WARNING: Can return a lot of data!
#
################################################

from oauth import oauth 
import httplib 

server_name = 'open.strands.com' 
api_uri = '/strandsapi/stream/'
NUM_DAYS = 1
DATE = '2009/08/24'

#Create our Consumer object 
consumer = oauth.OAuthConsumer('example_consumer_key',  
                               'example_consumer_secret') 

#For Two-legged OAuth, there is no access token 
#but it must still be present in request 
access_token = oauth.OAuthToken('', '') 

#Build our OAuth Request 
oauth_request = oauth.OAuthRequest.from_consumer_and_token( 
    consumer, 
    token=access_token,  
    http_method='GET', 
    http_url='https://%s%s' % (server_name, api_uri),  
    parameters={'abs_range': NUM_DAYS, 'end': DATE, 'type': 'workout'}) 

#Sign the Request 
oauth_request.sign_request(oauth.OAuthSignatureMethod_HMAC_SHA1(),  
                           consumer, access_token) 
 
conn = httplib.HTTPSConnection(server_name) 
api_uri_with_params = oauth_request.to_url() 
conn.request(oauth_request.http_method, api_uri_with_params) 

response = conn.getresponse() 

print "Request status: %s" % response.status 
print "Request response: %s" % response.read()