How to send oauth request using python-oauth2

I banged my head against the wall, trying to figure out how to send authenticated requests using oauth.

I managed to get access tokens, but I'm not quite sure how to send a request with them. I found this in twitter developer info:

https://dev.twitter.com/docs/auth/oauth/single-user-with-examples#python which has an 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 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: the user is the actual user (I removed it from the message), and tokenKey / tokenSecret are access tokens.

I thought that perhaps it was as simple as commenting on an offensive line, but there wasn’t 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, stackoverflow, you are my only hope! Anyone have any suggestions on using my access token to send a request?

thanks!

+6
source share
2 answers

The Twitter documentation is not outdated - the oauth2 version they link to is a 3 year old plug. For oauth2.Client.request

no longer force_auth_header argument for keyword <

The reason you still get errors even after deleting the violation line is because your default value for post_body is None , which is passed right before oauth2.Client.request . You cannot do this in practice. You will have to either require this argument, select the default value that works (for example, an empty string), or check before passing it to prevent this error.

+10
source

Just to expand the Rafe comments ...

Most oAuth libraries in Python are incredibly broken, outdated, and no longer supported. Most of them do not even pass their own tests, not to mention the format specification. Twitter should not link to any of these things, but they do.

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

in terms of oAuth 2, there is some recent work in this library trying to implement it (last fix 2 months ago) - https://github.com/idan/oauthlib , I did not try it myself, and found out about it yesterday.

to add quickly - even if you use etsy, you should be able to reverse engineer twython to use etsy endpoints if they support oauth1.

+4
source

Source: https://habr.com/ru/post/926443/


All Articles