我一直在苦苦思索如何使用 oauth 发送经过身份验证的请求。
我能够获得访问令牌,但不完全确定如何向他们提交请求。我在 twitter 的开发者信息中找到了这个:
https://dev.twitter.com/docs/auth/oauth/single-user-with-examples#python 其中有一些用于发送授权请求的示例代码:
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)
但是,当我包含所有信息时,我收到以下错误:
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'
其中 :user 是实际用户(我已将其从帖子中删除),而 tokenKey/tokenSecret 是访问令牌。
我想也许就像注释掉有问题的行一样简单,但没有这样的运气:
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
所以现在,stackoverflow,你是我唯一的希望!有人对如何使用我的访问令牌提交请求有建议吗?
谢谢!