使用 API 密钥进行 Urllib2 身份验证
我正在尝试连接到 radian6 api,它需要 auth_appkey、auth_user 和 auth_pass 作为MD5加密。
当我尝试使用 telnet 连接时,我可以成功获取响应 xml
telnet sandboxapi.radian6.com 80
Trying 142.166.170.31...
Connected to sandboxapi.radian6.com.
Escape character is '^]'.
GET /socialcloud/v1/auth/authenticate HTTP/1.1
host: sandboxapi.radian6.com
auth_appkey: 123456789
auth_user: [email protected]
auth_pass: 'md5encryptedpassword'
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Date: Thu, 26 Aug 2010 14:17:52 GMT
Content-Type: application/xml
Content-Length: 471
但是当我使用以下代码在 python 中尝试相同的操作时,
import urllib
import urllib2
user = '[email protected]'
password = 'md5encryptedpasswrod'
base_url = 'http://sandboxapi.radian6.com/'
api_key = '123456789'
pwman = urllib2.HTTPPasswordMgrWithDefaultRealm()
pwman.add_password(None, base_url, user, password)
auth_handler = urllib2.HTTPBasicAuthHandler(pwman)
opener = urllib2.build_opener(auth_handler)
urllib2.install_opener(opener)
req = urllib2.Request('http://sandboxapi.radian6.com/socialcloud/v1/auth/authenticate')
req.add_header('auth_appkey', api_key)
xml = urllib2.urlopen(req).read()
它会抛出以下错误跟踪,
Traceback (most recent call last):
File "connectapi.py", line 17, in <module>
xml = urllib2.urlopen(req).read()
File"/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/urllib2.py", line 126, in urlopen
return _opener.open(url, data, timeout)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/urllib2.py", line 397, in open
response = meth(req, response)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/urllib2.py", line 510, in http_response
'http', request, response, code, msg, hdrs)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/urllib2.py", line 435, in error
return self._call_chain(*args)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/urllib2.py", line 369, in _call_chain
result = func(*args)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/urllib2.py", line 518, in http_error_default
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 401: Unauthorized
我不知道我错过了什么。是 API 密钥还是 md5 加密密码导致我未经授权?
非常感谢您的智慧来挽救我的生命。
I am trying to connect to radian6 api, which requires the auth_appkey, auth_user and auth_pass as md5 encryption.
When I am trying to connect using telnet I can get the response xml successfully
telnet sandboxapi.radian6.com 80
Trying 142.166.170.31...
Connected to sandboxapi.radian6.com.
Escape character is '^]'.
GET /socialcloud/v1/auth/authenticate HTTP/1.1
host: sandboxapi.radian6.com
auth_appkey: 123456789
auth_user: [email protected]
auth_pass: 'md5encryptedpassword'
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Date: Thu, 26 Aug 2010 14:17:52 GMT
Content-Type: application/xml
Content-Length: 471
But when I am trying the same in python with the following code,
import urllib
import urllib2
user = '[email protected]'
password = 'md5encryptedpasswrod'
base_url = 'http://sandboxapi.radian6.com/'
api_key = '123456789'
pwman = urllib2.HTTPPasswordMgrWithDefaultRealm()
pwman.add_password(None, base_url, user, password)
auth_handler = urllib2.HTTPBasicAuthHandler(pwman)
opener = urllib2.build_opener(auth_handler)
urllib2.install_opener(opener)
req = urllib2.Request('http://sandboxapi.radian6.com/socialcloud/v1/auth/authenticate')
req.add_header('auth_appkey', api_key)
xml = urllib2.urlopen(req).read()
it is throwing the following error trace,
Traceback (most recent call last):
File "connectapi.py", line 17, in <module>
xml = urllib2.urlopen(req).read()
File"/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/urllib2.py", line 126, in urlopen
return _opener.open(url, data, timeout)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/urllib2.py", line 397, in open
response = meth(req, response)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/urllib2.py", line 510, in http_response
'http', request, response, code, msg, hdrs)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/urllib2.py", line 435, in error
return self._call_chain(*args)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/urllib2.py", line 369, in _call_chain
result = func(*args)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/urllib2.py", line 518, in http_error_default
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 401: Unauthorized
I don't know what I am missing. Is it the API key or md5 encrypted password that is why I am being unauthorized?
Your wisdom will be much appreciated to save my day.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在您的
telnet
会话中,您没有设置Authorization:
标头,但这就是HTTPBasicAuthHandler
使用的内容。 (您可以使用wireshark
或类似工具监听此内容。)大概该 API 不使用 HTTP 基本身份验证,而是使用其自制变体。您可能想要删除该行并手动设置 HTTP 标头。In your
telnet
session, you're not setting theAuthorization:
header, but that's whatHTTPBasicAuthHandler
uses. (You could listen in on this usingwireshark
or similar.) Presumably the API doesn't use HTTP Basic Authentication but its home-brew variant. You probably want to drop that line and set the HTTP headers manually.