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!
Twitter’s documentation is out of date — the version of
oauth2they link to is a 3-year-old fork. There is no keyword argumentforce_auth_headeranymore foroauth2.Client.requestThe reason why you still get errors even after removing the offending line is because your default value for
post_bodyisNonewhich gets passed straight through tooauth2.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.