1

我正在尝试从Algorithmia运行 Auto-Tag ,主要尝试使用cURLand Pyhton,并得到一个错误,指出:

卷曲

{"error":{"message":"authorization required"}}

Python

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

我究竟做错了什么?

谢谢。

4

2 回答 2

2

根据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.

于 2015-07-06T18:37:57.220 回答
0

PyPi 上可用的Algorithmia python 客户端允许直接设置 API 密钥:

import Algorithmia

input = ["rails","rails"]
client = Algorithmia.client('YOUR_API_KEY')
algo = client.algo('tags/AutoTagGithub/0.1.4')
print algo.pipe(input)
于 2017-05-09T00:00:30.137 回答