Skip to content

Commit ca383e4

Browse files
author
Ib Lundgren
committed
Updated README to use new OAuth class
1 parent 787a1a5 commit ca383e4

File tree

2 files changed

+21
-36
lines changed

2 files changed

+21
-36
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
*.pyc
22
*.sublime-project
33
docs/build
4+
*.swp

README.rst

+20-36
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,14 @@ both of the following:
77

88
1. They predate the `OAuth 1.0 spec`_, AKA RFC 5849.
99
2. They assume the usage of a specific HTTP request library.
10+
3. They only support use of HMAC-SHA1 signatures and authorization headers.
1011

1112
.. _`OAuth 1.0 spec`: http://tools.ietf.org/html/rfc5849
1213

1314
OAuthLib is a generic utility which implements the logic of OAuth without
14-
assuming a specific HTTP request object. Use it to graft OAuth support onto your
15-
favorite HTTP library. If you're a maintainer of such a library, write a thin
16-
veneer on top of OAuthLib and get OAuth support for very little effort.
17-
18-
OAuthLib will fully support OAuth 1.0 (RFC 5849).
15+
assuming a specific HTTP request object or workflow. Use it to graft OAuth support
16+
onto your favorite HTTP library. If you're a maintainer of such a library,
17+
write a thin veneer on top of OAuthLib and get OAuth support for very little effort.
1918

2019
HTTP Libraries supporting OAuthLib
2120
----------------------------------
@@ -49,7 +48,7 @@ There are three ways in which you can sign a request.
4948
Case study: Twitter
5049
-------------------
5150

52-
The study aims to show HTTP library developers how OAuthLib can be used. Using OAuthLib this way is not recommended. Instead use it indirectly through an HTTP library that uses OAuthLib, such as `requests`.
51+
**Objective: Show HTTP library developers how OAuthLib can be used.**
5352

5453
Twitter will give each client a consumer key and a consumer secret. The OAuth parameters are suplied in the authorization header and signed using HMAC-SHA1::
5554

@@ -78,15 +77,9 @@ The url is constructed by adding an ``oauth_token`` parameter to the request url
7877

7978
**Crafting the authorization header using OAuthLib**::
8079

81-
import oauthlib.oauth as oauth
82-
params = oauth.generate_params(client_key=client_key)
83-
signature = oauth.sign_hmac("GET", request_url, params, client_secret)
84-
params["oauth_signature"] = signature
85-
header = oauth.prepare_authorization_header(params)
86-
87-
*Use oauth.generate_params to create a dictionary of the required OAuth parameters. It will in addition to supplied parameters generate nonce, signature method, timestamp and version for you.*
88-
89-
*Note that any signature method (HMAC, SHA, Plaintext) requires type of request method, url and OAuth parameters. Client and token secrets are used with HMAC and RSA.*
80+
from oauthlib.oauth import OAuth
81+
oauth = OAuth(client_key=client_key, client_secret=client_secret)
82+
header = oauth.auth_header(request_url)
9083

9184
**Fetching the token using requests**::
9285

@@ -121,16 +114,12 @@ Fetch the access token
121114

122115
**Crafting the authorization header**::
123116

124-
callback = "http://whatever.you/registered"
125-
126-
import oauthlib.oauth as oauth
127-
params = oauth.generate_params(client_key=client_key,
128-
request_token=token,
129-
verifier=verifier,
130-
callback=callback)
131-
signature = oauth.sign_hmac("GET", access_url, params, client_secret)
132-
params["oauth_signature"] = signature
133-
header = oauth.prepare_authorization_header(params)
117+
from oauthlib.oauth import OAuth
118+
oauth = OAuth(client_key=client_key,
119+
client_secret=client_secret,
120+
request_token=token,
121+
verifier=verifier)
122+
header = oauth.auth_header(access_url)
134123

135124
**Fetching the token using requests**::
136125

@@ -152,22 +141,17 @@ Tweet hello world
152141
update_url = "'http://api.twitter.com/1/statuses/update.json"
153142
post = { 'status': "Hello world!", 'wrap_links': True }
154143

155-
import oauthlib.oauth as oauth
156-
params = oauth.generate_params(client_key=client_key,
157-
access_token=access_token)
158-
for k,v in post:
159-
params[k] = v
160-
signature = oauth.sign_hmac("GET", request_url, params,
161-
client_secret, token_secret)
162-
params["oauth_signature"] = signature
163-
header = oauth.prepare_authorization_header(params)
144+
from oauthlib.oauth import OAuth
145+
oauth = OAuth(client_key=client_key,
146+
client_secret=client_secret,
147+
token_secret=token_secret
148+
access_token=access_token)
149+
header = oauth.auth_header(update_url, post)
164150

165151
import requests
166152
headers = { "Authorization" : header }
167153
response = requests.post(update_url, post, headers=headers)
168154

169-
*Note that to create a correct signature the POST data will need to be included in params.*
170-
171155
License
172156
-------
173157

0 commit comments

Comments
 (0)