Skip to content

Instantly share code, notes, and snippets.

@node
Created September 21, 2012 02:04
Show Gist options
  • Save node/3759382 to your computer and use it in GitHub Desktop.
Save node/3759382 to your computer and use it in GitHub Desktop.
Python Oauth2 Client Demo
from rauth.service import OAuth2Service
import urllib2
#config
app_key='tonr'
app_secret='secret'
authorize_url='http://127.0.0.1:8080/TestSSOauth/oauth/authorize'
access_token_url='http://127.0.0.1:8080/TestSSOauth/oauth/token'
callback_url='http://127.0.0.1:8080/TCloudDiskApp/TCloudDiskApp/photos'
callback_url='www.baidu.com/s?wd='
test_resource_url='http://127.0.0.1:8080/TestSSOauth/photos/userInfo?format=json'
# request auth
s = OAuth2Service(name="sparklr",consumer_key='tonr',consumer_secret='secret',access_token_url=access_token_url,authorize_url=authorize_url)
url = s.get_authorize_url(redirect_url=callback_url,response_type='code')
# get code
print 'Open this url by browser: '
print url.replace('redirect_url','redirect_uri')
code = raw_input('Input the code : ')
# request token
data = dict(code=code,grant_type='authorization_code',redirect_uri=callback_url)
token = s.get_access_token('POST', data=data)
print 'access_token', token.content
access_token = token.content['access_token']
# visit resource
params = {"access_token":access_token}
res = s.request("GET",test_resource_url+"&access_token="+access_token)
print res.content
# END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment