Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send oauth request with python-oauth2

Tags:

python

oauth

I have been pounding my head against the wall over figuring out how to send authenticated requests with oauth.

I was able to get access tokens, but wasn't entirely sure how to submit a request with them. I found this on twitter's developer's information:

https://dev.twitter.com/docs/auth/oauth/single-user-with-examples#python which has some example code for sending an authorized request:

def oauth_req(url, key, secret, http_method="GET", post_body=None,http_headers=None):
    consumer = oauth.Consumer(key=consumerKey, secret=consumerSecret)
    token = oauth.Token(key=tokenKey, secret=tokenSecret)
    client = oauth.Client(consumer, token)
    resp, content = client.request(
        url,
        method=http_method,
        body=post_body,
        headers=http_headers,
        #force_auth_header=True                                                                                                                                                 
        )
    return resp,content

oauth_req('http://openapi.etsy.com/v2/shops/:user/transactions',tokenKey,tokenSecret)

However, when I included all of my information, I received the following error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/tmp/python-22060Umg.py", line 153, in <module>
    transactions = oauth_req('http://openapi.etsy.com/v2/shops/:user/transactions',tokenKey,tokenSecret)
  File "/tmp/python-22060Umg.py", line 76, in oauth_req
    force_auth_header=True
TypeError: request() got an unexpected keyword argument 'force_auth_header'

where :user is the actual user (I've removed it from the post) and tokenKey/tokenSecret are the access tokens.

I thought perhaps it was as simple as commenting out the offending line, but no such luck:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/tmp/python-22060hwm.py", line 153, in <module>
    transactions = oauth_req('http://openapi.etsy.com/v2/shops/:user/transactions',tokenKey,tokenSecret)
  File "/tmp/python-22060hwm.py", line 75, in oauth_req
    headers=http_headers
  File "/usr/lib/python2.7/dist-packages/oauth2/__init__.py", line 662, in request
    req.sign_request(self.method, self.consumer, self.token)
  File "/usr/lib/python2.7/dist-packages/oauth2/__init__.py", line 493, in sign_request
    self['oauth_body_hash'] = base64.b64encode(sha(self.body).digest())
TypeError: must be string or buffer, not None

So now, stackoverflow, you are my only hope! Anyone has suggestions for how to use my access token to submit a request?

thanks!

like image 406
nathan lachenmyer Avatar asked Sep 27 '12 18:09

nathan lachenmyer


2 Answers

Twitter's documentation is out of date -- the version of oauth2 they link to is a 3-year-old fork. There is no keyword argument force_auth_header anymore for oauth2.Client.request

The reason why you still get errors even after removing the offending line is because your default value for post_body is None which gets passed straight through to oauth2.Client.request. You won't be able to do that in practice. You'll either have to require that argument, pick a default value that works (e.g an empty string), or check before you pass it through to prevent this error.

like image 72
Rafe Kettler Avatar answered Sep 28 '22 08:09

Rafe Kettler


Just to expand on Rafe's comments...

The bulk of oAuth libraries in Python are incredibly broken, out of date, and no longer maintained. Most of them don't even pass their own tests, let alone the format specification. Twitter shouldn't be referencing any of that stuff, but they do.

Twython is a Python twitter client that wraps oAuth - https://github.com/ryanmcgrath/twython - it wraps the oAuth1 spec via the requests and requests-oauth packages - http://docs.python-requests.org/en/latest/ , http://pypi.python.org/pypi/requests-oauth - but Twitter's API accepts that.

in terms of oAuth 2, this library has some recent work trying to implement it (last commit 2 months ago) -- https://github.com/idan/oauthlib . i haven't tried it myself, and just found out about it yesterday.

to add quickly - even though you're using etsy, you should be able to reverse engineer twython to use etsy endpoints if they're supporting oauth1.

like image 36
Jonathan Vanasco Avatar answered Sep 28 '22 06:09

Jonathan Vanasco