Skip to content

Instantly share code, notes, and snippets.

@captDaylight
Created December 10, 2011 17:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save captDaylight/1455637 to your computer and use it in GitHub Desktop.
Save captDaylight/1455637 to your computer and use it in GitHub Desktop.
for stack
def authenticate(request):
# STEP 1: Generate a request token
post_url = '/auth/reqtoken'
params = urllib.urlencode({'client_id': client_id, 'client_secret':client_secret, 'type':'app'})
data = urllib2.urlopen('%s%s' % (base_url, post_url), params).read()
data = json.loads(data)
token = data['request_token']
print "here is the token"
url = "https://gimmebar.com/authorize?client_id="+client_id+"&token="+token+"&response_type=code"
updates = {'url': url, 'token':token}
# STEP 2:
return HttpResponse(json.dumps(updates), mimetype="application/json")
def exchange(request):
token = request.GET.get('token', '')
# STEP 3: Exchange the request token for an authorization token when the user returns
post_url = '/auth/exchange/request'
params = urllib.urlencode({'client_id': client_id, 'token':token, 'response_type':'code'})
data = urllib2.urlopen('%s%s' % (base_url, post_url), params).read()
data = json.loads(data)
code = data['code']
# STEP 4: Exchange the authorization token for an access token
post_url = '/auth/exchange/authorization'
params = urllib.urlencode({'code': code,'grant_type':'authorization_code'})
data = urllib2.urlopen('%s%s' % (base_url, post_url), params).read()
data = json.loads(data)
access_token = data['access_token']
# STEP 5: Use the access token to authenticate with the API and retrieve user data
post_url = '/tags'
headers = {'Authorization': 'Bearer %s' % access_token}
query = '/collections'
req = urllib2.Request('%s%s%s' % (base_url, post_url, query), headers=headers)
response = urllib2.urlopen(req)
the_page = response.read()
return render_to_response('graphs/exchange.html',context_instance=RequestContext(request))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment