Feeds. python oauth2 problem and solution

72 views
Skip to first unread message

Chris aQD

unread,
Mar 14, 2013, 9:39:56 AM3/14/13
to 7digit...@googlegroups.com
Downloading 7digital feeds using pythons oauth2 does not work.
This is because 7digital does not accept an oauth_body_hash in the query.
Since oauth_body_hash is an unofficial extension to the oauth standard, it will fail elsewhere as well.

To fix this, go to your oauth2 directory, which is probably something like /usr/lib/python2.7/dist-packages/oauth2/.
Edit __init__.py :
On line 493 you will find the following line:
             self['oauth_body_hash'] = base64.b64encode(sha(self.body).digest())

Comment this out and add pass under it, resulting in this:
             #self['oauth_body_hash'] = base64.b64encode(sha(self.body).digest())
             pass

It is however, probably a better idea to keep oauth in your program directory, and not use the one supplied by the linux distro you are using.
Any changes you make to oauth2 will be overwritten when you upgrade if you use the version that comes with you operating system.

Regards,
Chris Q.

filip

unread,
Mar 14, 2013, 8:17:16 PM3/14/13
to 7digit...@googlegroups.com
Hi Chris,

thanks for sharing this.

I came across the same problem recently when trying to help a python developer generate oauth signed streaming URLs. After a bit of googling and digging around in the oauth2 code I've found out that adding an extra parameter is_form_encoded=True when creating the oauth request gets rid of the problem; see the full code below.

If you do end up trying this approach let me know if it worked for you as well or not.

Cheers
Filip

----

import oauth2 as oauth

consumer = oauth.Consumer("YOUR_API_KEY", "YOUR_API_SECRET")

req = oauth.Request(method="GET", url=request_url, is_form_encoded=True)

req['oauth_timestamp'] = oauth.Request.make_timestamp()
req['oauth_nonce'] = oauth.Request.make_nonce()

req.sign_request(oauth.SignatureMethod_HMAC_SHA1(), consumer, token=None)

print req.to_url()

Jason Rubenstein

unread,
Mar 21, 2013, 3:25:10 PM3/21/13
to 7digit...@googlegroups.com
Alternatively: 

You can use an instance of oauth.Client to make the request. 

consumer = oauth.Consumer(oauthkey, secret)
client = oauth.Client(consumer)
response, content = client.request(
    REQUEST_TOKEN_URL,
    headers = {"Content-Type":"application/x-www-form-urlencoded"}
)


There's a caveat here: the request() method of Client defaults to an http method of GET,  and will only default to Content-Type : application/x-www-form-urlencoded if the http method is a POST. That's why I'm passing the headers up there in my example. I think that the REST API will accept a POST, in which case the headers argument is redundant.

Have a look at the code in oauth2 starting around line 615 and at 639.
Reply all
Reply to author
Forward
0 new messages