雅虎BOSS V2授权问题
我在雅虎的身份验证/授权方面遇到了非常困难的情况。我已在帐户中启用了 BOSS,设置了付款方式,现在我尝试使用一些 Python 代码运行搜索:
import urllib2
import oauth2 as oauth
import time
OAUTH_CONSUMER_KEY = "blahblahblah"
OAUTH_CONSUMER_SECRET = "blah"
def oauth_request(url, params, method="GET"):
params['oauth_version'] = "1.0",
params['oauth_nonce'] = oauth.generate_nonce(),
params['oauth_timestamp'] = int(time.time())
consumer = oauth.Consumer(key=OAUTH_CONSUMER_KEY,
secret=OAUTH_CONSUMER_SECRET)
params['oauth_consumer_key'] = consumer.key
req = oauth.Request(method=method, url=url, parameters=params)
req.sign_request(oauth.SignatureMethod_HMAC_SHA1(), consumer, None)
return req
if __name__ == "__main__":
url = "http://yboss.yahooapis.com/ysearch/web"
req = oauth_request(url, params={"q": "cats dogs"})
req_url = req.to_url()
print req_url
result = urllib2.urlopen(req_url)
我不断收到 urllib2.HTTPError: HTTP Error 401: Unauthorized
例外。我无法弄清楚我的密钥或签名方法是否有问题,或者我是否在签名后以某种方式篡改了我的数据,或者交易是什么。有人有建议吗?
I'm having an awfully hard time with Yahoo's authentication/authorization. I've enabled BOSS in my account, set up a payment method, and now I'm trying to run a search using some python code:
import urllib2
import oauth2 as oauth
import time
OAUTH_CONSUMER_KEY = "blahblahblah"
OAUTH_CONSUMER_SECRET = "blah"
def oauth_request(url, params, method="GET"):
params['oauth_version'] = "1.0",
params['oauth_nonce'] = oauth.generate_nonce(),
params['oauth_timestamp'] = int(time.time())
consumer = oauth.Consumer(key=OAUTH_CONSUMER_KEY,
secret=OAUTH_CONSUMER_SECRET)
params['oauth_consumer_key'] = consumer.key
req = oauth.Request(method=method, url=url, parameters=params)
req.sign_request(oauth.SignatureMethod_HMAC_SHA1(), consumer, None)
return req
if __name__ == "__main__":
url = "http://yboss.yahooapis.com/ysearch/web"
req = oauth_request(url, params={"q": "cats dogs"})
req_url = req.to_url()
print req_url
result = urllib2.urlopen(req_url)
I keep getting a urllib2.HTTPError: HTTP Error 401: Unauthorized
exception. I can't figure out whether there's something wrong with my key, or the method of signing, or if I'm somehow tampering with my data after signing, or what the deal is. Anyone have suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我做了一些小更改以使您的示例正常工作。注释见代码。
I made some small changes to make your example work. See code for comments.
这是一个 Python 代码片段,适用于我对抗 Yahoo! BOSS:
我使用身份验证标头来提交 OAuth 签名。
Here is a Python code snippet that works for me against Yahoo! BOSS:
Im using an Authenticate Header to submit the OAuth signature.
所以我决定放弃 Python 并尝试 Perl,而且它确实有效。这是一个最小的代码示例:
So I decided to ditch Python and try Perl, and it Just Worked. Here's a minimal code sample:
这是另一个解决方案,这次回到了 python-land。这是由 Pattern 网络挖掘工具包的作者 Tom De Smedt 整理的。
我会与
python-oauth2
的作者沟通,看看是否可以修复。Here's another solution, this time back in python-land. This was put together by Tom De Smedt, author of the Pattern web-mining kit.
I'll communicate with the author of
python-oauth2
to see if it can be fixed.这是访问 Yahoo! 的示例代码BOSS API v2 使用 python-oauth 作为 oauth 库。
Here is sample code to access Yahoo! BOSS API v2 using with python-oauth as oauth liberary.
我使用调试器进入 urllib2.open 代码,发现响应具有以下标头:
所以我猜我遇到了某种 OAuth 版本不匹配的情况。
I stepped into the
urllib2.open
code using the debugger, and found that the response has this header:So I guess I'm having some kind of version mismatch of OAuth.