我一直在撞墙,试图找出如何用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)
Run Code Online (Sandbox Code Playgroud)
但是,当我包含所有信息时,我收到以下错误:
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'
Run Code Online (Sandbox Code Playgroud)
其中: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
Run Code Online (Sandbox Code Playgroud)
所以现在,stackoverflow,你是我唯一的希望!有人建议如何使用我的访问令牌提交请求?
谢谢!
Raf*_*ler 10
Twitter的文档已经过时 - oauth2它们链接的版本是一个3年前的版本.没有关键字参数force_auth_header了oauth2.Client.request
你甚至在删除违规行之后仍然会出现错误的原因是因为你的默认值post_body是None直接传递给的oauth2.Client.request.在实践中你将无法做到这一点.你要么必须要求那个参数,选择一个有效的默认值(例如一个空字符串),或者在传递它之前检查以防止这个错误.