Skip to content

Instantly share code, notes, and snippets.

@dajobe
Created December 28, 2011 23:13
Show Gist options
  • Save dajobe/1530346 to your computer and use it in GitHub Desktop.
Save dajobe/1530346 to your computer and use it in GitHub Desktop.
Digg OAuth flow example in python
#! /usr/bin/env python
#
# Go through the Digg OAuth flow for an application and user
# to get back Digg OAuth access token + access token secret
#
# This code is in the Public Domain
#
try:
from cgi import parse_qs
except e:
from urlparse import parse_qs
import sys
import oauth2
from sys import exit
# Configuration
#
# Get the application key and secret from http://developers.digg.com/ "My Apps"
#
app_key = 'APP_KEY'
app_secret = 'APP_SECRET'
site_base_uri = 'http://digg.com'
api_base_uri = 'http://services.digg.com'
request_token_uri = api_base_uri + "/oauth/request_token"
access_token_uri = api_base_uri + "/oauth/access_token"
# Initialization
consumer = oauth2.Consumer(key=app_key, secret=app_secret)
client = oauth2.Client(consumer)
# Step 1: Get a request token (temporary token) used for having the user
# authorize an access token to sign the request to obtain the access toekn
print "POSTing to %s" % (request_token_uri, )
resp, content = client.request(request_token_uri, "POST")
print resp
print "Request token content: '%s'" % (content, )
if resp['status'] != '200':
print "Request failed"
sys.exit(1)
q = parse_qs(content)
request_token = q['oauth_token'][0]
request_token_secret = q['oauth_token_secret'][0]
print "Request token '%s' secret '%s'" % (request_token, request_token_secret)
# Step 2: Redirect / OOB to get the user to auth the url and enter the verifier
auth_uri = '%s/oauth/authorize?oauth_token=%s' % (site_base_uri, request_token, )
print "Authorization URL is %s" % (auth_uri, )
sys.stdout.write('Type the verification number: ')
verifier = sys.stdin.readline().rstrip('\r\n')
print "verifier is %s" % (verifier, )
# Step 3: Request the access token the user has approved
token = oauth2.Token(request_token, request_token_secret)
token.set_verifier(verifier)
client = oauth2.Client(consumer, token)
resp, content = client.request(access_token_uri, "POST")
print "Access token content: '%s'" % (content, )
q = parse_qs(content)
access_token = q['oauth_token'][0]
access_token_secret = q['oauth_token_secret'][0]
print "Access token '%s' secret '%s'" % (access_token, access_token_secret)
sys.exit(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment