Skip to content

Commit

Permalink
Update documentation of sample oauth application.
Browse files Browse the repository at this point in the history
  • Loading branch information
abierbaum committed Dec 28, 2014
1 parent 7cb4189 commit 05d2722
Showing 1 changed file with 11 additions and 15 deletions.
26 changes: 11 additions & 15 deletions renamer_app/test_oauth.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import requests
import json
import os
import requests

from urlparse import parse_qs

from requests_oauthlib import OAuth1

Expand All @@ -19,39 +21,33 @@

# Check if we already have access token and secret
if not os.path.exists(TOKEN_FILE):
# Get the anonymous oauth_token and secret
# 1) Obtain Request token
oauth = OAuth1(API_KEY, client_secret=API_KEY_SECRET, callback_uri='oob')
r = requests.post(url=REQUEST_TOKEN_URL, auth=oauth)
print "Content: ", r.content

from urlparse import parse_qs
credentials = parse_qs(r.content)
resource_owner_key = credentials.get('oauth_token')[0]
resource_owner_secret = credentials.get('oauth_token_secret')[0]

print "owner key: ", resource_owner_key
print "owner secret: ", resource_owner_secret

# 2) Obtain authorization for the user to access resources
# Redirect the user to /authorize and get the callback
authorize_url = AUTHORIZE_URL + '?oauth_token=' + resource_owner_key + '&oauth_consumer_key=' + API_KEY
authorize_url += "&Access=Full&Permissions=Modify"

print 'Please go here and authorize,', authorize_url
verifier = raw_input('Please enter the six-digit PIN code: ')

# Get the final oauth_token and secret
# 3) Obtain final access token
oauth = OAuth1(API_KEY, client_secret=API_KEY_SECRET,
resource_owner_key=resource_owner_key,
resource_owner_secret=resource_owner_secret,
verifier=verifier)
r = requests.post(url=ACCESS_TOKEN_URL, auth=oauth)
print "Access content", r.content
# OUT: 'oauth_token=2f227cce369edad1ff3880bb4dab84f2&oauth_token_secret=10e284c0ce48c2c2c6ce4f58fca358d6ff495a55&oauth_callback_confirmed=true'

credentials = parse_qs(r.content)
access_token = credentials.get('oauth_token')[0]
access_token_secret = credentials.get('oauth_token_secret')[0]

# Store access token so we can use it later
with open(TOKEN_FILE, 'w') as f:
json.dump({'access_token': access_token,
'access_token_secret': access_token_secret}, f)
Expand All @@ -66,18 +62,18 @@
# Make authenticated calls to the API
oauth = OAuth1(API_KEY, client_secret=API_KEY_SECRET,
resource_owner_key=access_token,
resource_owner_secret=access_token_secret)
resource_owner_secret=access_token_secret,
signature_type='auth_header')

# Get authuser details
url = BASE_URL + '!authuser'
r = requests.get(url=url, auth=oauth, headers={"Accept": 'application/json'})
#print r.content

print "Authuser"
print json.dumps(r.json(), indent = 2)

# get known user details
url = API_ORIGIN + '/api/v2/user/cmac'
r = requests.get(url=url, auth=oauth, headers={"Accept": 'application/json'})

print "cmac user"
print json.dumps(r.json(), indent = 2)

0 comments on commit 05d2722

Please sign in to comment.