@@ -48,91 +48,87 @@ This code was originally forked from [Leah Culver and Andy Smith's oauth.py code
48
48
49
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).
50
50
51
- <pre ><code >
52
- import oauth2 as oauth
53
-
54
- # Create your consumer with the proper key/secret.
55
- consumer = oauth.Consumer(key="your-twitter-consumer-key",
56
- secret="your-twitter-consumer-secret")
57
-
58
- # Request token URL for Twitter.
59
- request_token_url = "http://twitter.com/oauth/request_token"
60
-
61
- # Create our client.
62
- client = oauth.Client(consumer)
63
-
64
- # The OAuth Client request works just like httplib2 for the most part.
65
- resp, content = client.request(request_token_url, "GET")
66
- print resp
67
- print content
68
- </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
69
67
70
68
# Twitter Three-legged OAuth Example
71
69
72
70
Below is an example of how one would go through a three-legged OAuth flow to
73
71
gain access to protected resources on Twitter. This is a simple CLI script, but
74
72
can be easily translated to a web application.
75
73
76
- <pre ><code >
77
- import urlparse
78
- import oauth2 as oauth
79
-
80
- consumer_key = 'my_key_from_twitter'
81
- consumer_secret = 'my_secret_from_twitter'
82
-
83
- request_token_url = 'http://twitter.com/oauth/request_token'
84
- access_token_url = 'http://twitter.com/oauth/access_token'
85
- authorize_url = 'http://twitter.com/oauth/authorize'
86
-
87
- consumer = oauth.Consumer(consumer_key, consumer_secret)
88
- client = oauth.Client(consumer)
89
-
90
- # Step 1: Get a request token. This is a temporary token that is used for
91
- # having the user authorize an access token and to sign the request to obtain
92
- # said access token.
93
-
94
- resp, content = client.request(request_token_url, "GET")
95
- if resp['status'] != '200':
96
- raise Exception("Invalid response %s." % resp['status'])
97
-
98
- request_token = dict(urlparse.parse_qsl(content))
99
-
100
- print "Request Token:"
101
- print " - oauth_token = %s" % request_token['oauth_token']
102
- print " - oauth_token_secret = %s" % request_token['oauth_token_secret']
103
- print
104
-
105
- # Step 2: Redirect to the provider. Since this is a CLI script we do not
106
- # redirect. In a web application you would redirect the user to the URL
107
- # below.
108
-
109
- print "Go to the following link in your browser:"
110
- print "%s?oauth_token=%s" % (authorize_url, request_token['oauth_token'])
111
- print
112
-
113
- # After the user has granted access to you, the consumer, the provider will
114
- # redirect you to whatever URL you have told them to redirect to. You can
115
- # usually define this in the oauth_callback argument as well.
116
- accepted = 'n'
117
- while accepted.lower() == 'n':
118
- accepted = raw_input('Have you authorized me? (y/n) ')
119
-
120
- # Step 3: Once the consumer has redirected the user back to the oauth_callback
121
- # URL you can request the access token the user has approved. You use the
122
- # request token to sign this request. After this is done you throw away the
123
- # request token and use the access token returned. You should store this
124
- # access token somewhere safe, like a database, for future use.
125
- token = oauth.Token(request_token['oauth_token'],
126
- request_token['oauth_token_secret'])
127
- client = oauth.Client(consumer, token)
128
-
129
- resp, content = client.request(access_token_url, "POST")
130
- access_token = dict(urlparse.parse_qsl(content))
131
-
132
- print "Access Token:"
133
- print " - oauth_token = %s" % access_token['oauth_token']
134
- print " - oauth_token_secret = %s" % access_token['oauth_token_secret']
135
- print
136
- print "You may now access protected resources using the access tokens above."
137
- print
138
- </code ></pre >
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