Skip to content

Commit df158fc

Browse files
committed
Added a verbose example of three-legged OAuth using Twitter.
1 parent bde974c commit df158fc

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed

README.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,73 @@ resp, content = client.request(request_token_url, "GET")
6868
print resp
6969
print content
7070
</code></pre>
71+
72+
# Twitter Three-legged OAuth Example
73+
74+
Below is an example of how one would go through a three-legged OAuth flow to
75+
gain access to protected resources on Twitter. This is a simple CLI script, but
76+
can be easily translated to a web application.
77+
78+
<pre><code>
79+
import urlparse
80+
import oauth2 as oauth
81+
82+
consumer_key = 'my_key_from_twitter'
83+
consumer_secret = 'my_secret_from_twitter'
84+
85+
request_token_url = 'http://twitter.com/oauth/request_token'
86+
access_token_url = 'http://twitter.com/oauth/access_token'
87+
authorize_url = 'http://twitter.com/oauth/authorize'
88+
89+
consumer = oauth.Consumer(consumer_key, consumer_secret)
90+
client = oauth.Client(consumer)
91+
92+
# Step 1: Get a request token. This is a temporary token that is used for
93+
# having the user authorize an access token and to sign the request to obtain
94+
# said access token.
95+
96+
resp, content = client.request(request_token_url, "GET")
97+
if resp['status'] != '200':
98+
raise Exception("Invalid response %s." % resp['status'])
99+
100+
request_token = dict(urlparse.parse_qsl(content))
101+
102+
print "Request Token:"
103+
print " - oauth_token = %s" % request_token['oauth_token']
104+
print " - oauth_token_secret = %s" % request_token['oauth_token_secret']
105+
print
106+
107+
# Step 2: Redirect to the provider. Since this is a CLI script we do not
108+
# redirect. In a web application you would redirect the user to the URL
109+
# below.
110+
111+
print "Go to the following link in your browser:"
112+
print "%s?oauth_token=%s" % (authorize_url, request_token['oauth_token'])
113+
print
114+
115+
# After the user has granted access to you, the consumer, the provider will
116+
# redirect you to whatever URL you have told them to redirect to. You can
117+
# usually define this in the oauth_callback argument as well.
118+
accepted = 'n'
119+
while accepted.lower() == 'n':
120+
accepted = raw_input('Have you authorized me? (y/n) ')
121+
122+
# Step 3: Once the consumer has redirected the user back to the oauth_callback
123+
# URL you can request the access token the user has approved. You use the
124+
# request token to sign this request. After this is done you throw away the
125+
# request token and use the access token returned. You should store this
126+
# access token somewhere safe, like a database, for future use.
127+
token = oauth.Token(request_token['oauth_token'],
128+
request_token['oauth_token_secret'])
129+
client = oauth.Client(consumer, token)
130+
131+
resp, content = client.request(access_token_url, "POST")
132+
access_token = dict(urlparse.parse_qsl(content))
133+
134+
print "Access Token:"
135+
print " - oauth_token = %s" % access_token['oauth_token']
136+
print " - oauth_token_secret = %s" % access_token['oauth_token_secret']
137+
print
138+
print "You may now access protected resources using the access tokens above."
139+
print
140+
</code></pre>

0 commit comments

Comments
 (0)