@@ -11,60 +11,124 @@ This code was originally forked from [Leah Culver and Andy Smith's oauth.py code
11
11
12
12
# Signing a Request
13
13
14
- <pre ><code >
15
- import oauth2 as oauth
16
- import time
17
-
18
- # Set the API endpoint
19
- url = "http://example.com/photos"
20
-
21
- # Set the base oauth_* parameters along with any other parameters required
22
- # for the API call.
23
- params = {
24
- 'oauth_version': "1.0",
25
- 'oauth_nonce': oauth.generate_nonce(),
26
- 'oauth_timestamp': int(time.time())
27
- 'user': 'joestump',
28
- 'photoid': 555555555555
29
- }
30
-
31
- # Set up instances of our Token and Consumer. The Consumer.key and
32
- # Consumer.secret are given to you by the API provider. The Token.key and
33
- # Token.secret is given to you after a three-legged authentication.
34
- token = oauth.Token(key="tok-test-key", secret="tok-test-secret")
35
- consumer = oauth.Consumer(key="con-test-key", secret="con-test-secret")
36
-
37
- # Set our token/key parameters
38
- params['oauth_token'] = tok.key
39
- params['oauth_consumer_key'] = con.key
40
-
41
- # Create our request. Change method, etc. accordingly.
42
- req = oauth.Request(method="GET", url=url, parameters=params)
43
-
44
- # Sign the request.
45
- signature_method = oauth.SignatureMethod_HMAC_SHA1()
46
- req.sign_request(signature_method, consumer, token)
47
- </code ></pre >
14
+ import oauth2 as oauth
15
+ import time
16
+
17
+ # Set the API endpoint
18
+ url = "http://example.com/photos"
19
+
20
+ # Set the base oauth_* parameters along with any other parameters required
21
+ # for the API call.
22
+ params = {
23
+ 'oauth_version': "1.0",
24
+ 'oauth_nonce': oauth.generate_nonce(),
25
+ 'oauth_timestamp': int(time.time())
26
+ 'user': 'joestump',
27
+ 'photoid': 555555555555
28
+ }
29
+
30
+ # Set up instances of our Token and Consumer. The Consumer.key and
31
+ # Consumer.secret are given to you by the API provider. The Token.key and
32
+ # Token.secret is given to you after a three-legged authentication.
33
+ token = oauth.Token(key="tok-test-key", secret="tok-test-secret")
34
+ consumer = oauth.Consumer(key="con-test-key", secret="con-test-secret")
35
+
36
+ # Set our token/key parameters
37
+ params['oauth_token'] = tok.key
38
+ params['oauth_consumer_key'] = con.key
39
+
40
+ # Create our request. Change method, etc. accordingly.
41
+ req = oauth.Request(method="GET", url=url, parameters=params)
42
+
43
+ # Sign the request.
44
+ signature_method = oauth.SignatureMethod_HMAC_SHA1()
45
+ req.sign_request(signature_method, consumer, token)
48
46
49
47
# Using the Client
50
48
51
49
The <code >oauth2.Client</code > is based on <code >httplib2</code > and works just as you'd expect it to. The only difference is the first two arguments to the constructor are an instance of <code >oauth2.Consumer</code > and <code >oauth2.Token</code > (<code >oauth2.Token</code > is only needed for three-legged requests).
52
50
53
- <pre ><code >
54
- import oauth2 as oauth
55
-
56
- # Create your consumer with the proper key/secret.
57
- consumer = oauth.Consumer(key="your-twitter-consumer-key",
58
- secret="your-twitter-consumer-secret")
59
-
60
- # Request token URL for Twitter.
61
- request_token_url = "http://twitter.com/oauth/request_token"
62
-
63
- # Create our client.
64
- client = oauth.Client(consumer)
65
-
66
- # The OAuth Client request works just like httplib2 for the most part.
67
- resp, content = client.request(request_token_url, "GET")
68
- print resp
69
- print content
70
- </code ></pre >
51
+ import oauth2 as oauth
52
+
53
+ # Create your consumer with the proper key/secret.
54
+ consumer = oauth.Consumer(key="your-twitter-consumer-key",
55
+ secret="your-twitter-consumer-secret")
56
+
57
+ # Request token URL for Twitter.
58
+ request_token_url = "http://twitter.com/oauth/request_token"
59
+
60
+ # Create our client.
61
+ client = oauth.Client(consumer)
62
+
63
+ # The OAuth Client request works just like httplib2 for the most part.
64
+ resp, content = client.request(request_token_url, "GET")
65
+ print resp
66
+ print content
67
+
68
+ # Twitter Three-legged OAuth Example
69
+
70
+ Below is an example of how one would go through a three-legged OAuth flow to
71
+ gain access to protected resources on Twitter. This is a simple CLI script, but
72
+ can be easily translated to a web application.
73
+
74
+ import urlparse
75
+ import oauth2 as oauth
76
+
77
+ consumer_key = 'my_key_from_twitter'
78
+ consumer_secret = 'my_secret_from_twitter'
79
+
80
+ request_token_url = 'http://twitter.com/oauth/request_token'
81
+ access_token_url = 'http://twitter.com/oauth/access_token'
82
+ authorize_url = 'http://twitter.com/oauth/authorize'
83
+
84
+ consumer = oauth.Consumer(consumer_key, consumer_secret)
85
+ client = oauth.Client(consumer)
86
+
87
+ # Step 1: Get a request token. This is a temporary token that is used for
88
+ # having the user authorize an access token and to sign the request to obtain
89
+ # said access token.
90
+
91
+ resp, content = client.request(request_token_url, "GET")
92
+ if resp['status'] != '200':
93
+ raise Exception("Invalid response %s." % resp['status'])
94
+
95
+ request_token = dict(urlparse.parse_qsl(content))
96
+
97
+ print "Request Token:"
98
+ print " - oauth_token = %s" % request_token['oauth_token']
99
+ print " - oauth_token_secret = %s" % request_token['oauth_token_secret']
100
+ print
101
+
102
+ # Step 2: Redirect to the provider. Since this is a CLI script we do not
103
+ # redirect. In a web application you would redirect the user to the URL
104
+ # below.
105
+
106
+ print "Go to the following link in your browser:"
107
+ print "%s?oauth_token=%s" % (authorize_url, request_token['oauth_token'])
108
+ print
109
+
110
+ # After the user has granted access to you, the consumer, the provider will
111
+ # redirect you to whatever URL you have told them to redirect to. You can
112
+ # usually define this in the oauth_callback argument as well.
113
+ accepted = 'n'
114
+ while accepted.lower() == 'n':
115
+ accepted = raw_input('Have you authorized me? (y/n) ')
116
+
117
+ # Step 3: Once the consumer has redirected the user back to the oauth_callback
118
+ # URL you can request the access token the user has approved. You use the
119
+ # request token to sign this request. After this is done you throw away the
120
+ # request token and use the access token returned. You should store this
121
+ # access token somewhere safe, like a database, for future use.
122
+ token = oauth.Token(request_token['oauth_token'],
123
+ request_token['oauth_token_secret'])
124
+ client = oauth.Client(consumer, token)
125
+
126
+ resp, content = client.request(access_token_url, "POST")
127
+ access_token = dict(urlparse.parse_qsl(content))
128
+
129
+ print "Access Token:"
130
+ print " - oauth_token = %s" % access_token['oauth_token']
131
+ print " - oauth_token_secret = %s" % access_token['oauth_token_secret']
132
+ print
133
+ print "You may now access protected resources using the access tokens above."
134
+ print
0 commit comments