我正在尝试从Algorithmia运行自动标记,并且主要尝试使用cURL
和Pyhton
,并得到一个指出以下错误:
卷曲
{"error":{"message":"authorization required"}}
蟒蛇
Last login: Mon Jul 6 20:27:54 on ttys000
cd '/Users/abc/Desktop/' && '/usr/bin/pythonw' '/Users/abc/Desktop/autoTag.py' && echo Exit status: $? && exit 1
abc-MacBook-Pro:~ abc$ cd '/Users/abc/Desktop/' && '/usr/bin/pythonw' '/Users/abc/Desktop/autoTag.py' && echo Exit status: $? && exit 1
Traceback (most recent call last):
File "/Users/abc/Desktop/autoTag.py", line 6, in <module>
response = urllib2.urlopen(request, json.dumps(input))
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 127, in urlopen
return _opener.open(url, data, timeout)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 410, in open
response = meth(req, response)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 523, in http_response
'http', request, response, code, msg, hdrs)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 448, in error
return self._call_chain(*args)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 382, in _call_chain
result = func(*args)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 531, in http_error_default
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 401: Unauthorized
我究竟做错了什么?
谢谢。
最佳答案
根据documentation for AutoTag,在发送请求之前,您希望将授权信息作为标头包括在内。
这是他们的例子:
import urllib2, json
input = ["rails","rails"]
request = urllib2.Request('https://api.algorithmia.com/v1/algo/tags/AutoTagGithub/0.1.1')
request.add_header('Content-Type', 'application/json')
request.add_header('Authorization', 'Simple sim11111111111111111123456789abcdef')
response = urllib2.urlopen(request, json.dumps(input))
print response.read()
如果您遵循他们的示例(看起来像您一样),建议您使用自己的令牌代替
sim11111111111111111123456789abcdef
。