我有下面的代码,它应该在github上执行创建新下载的第一部分。它应该发送带有POST的json数据。
jsonstring = '{"name": "test", "size": "4"}'
req = urllib2.Request("https://api.github.com/repos/<user>/<repo>/downloads")
req.add_header('Authorization', 'token ' + '<token>')
result = urllib2.urlopen(req, jsonstring)如果我将, jsonstring从urlopen()中删除,它不会失败,并给出可用下载的列表。但是,如果我试图发布json-字符串,则会得到404错误。
问题必须是在json,或在我发送它的方式,但我不知道是什么问题。<...>上的字符串在实际代码中是正确的,我只是从文章中删除了它们
我在命令行上对curl进行了大致相同的尝试,使用了稍微不同的身份验证方法,而且它起了作用。
测试:
Works(返回被通缉的json):
curl -u "user:password" --data "json..." https://api.github.com/repos/<user>/<repo>/downloads作品:
curl -H 'Authorization: token <token>' https://api.github.com/不工作(返回“无效凭据”):
curl -H 'Authorization: token <invalid_token>' https://api.github.com/不起作用(“找不到”):
curl -H 'Authorization: token <valid_token>' --data "json..." https://api.github.com/repos/<user>/<repo>/downloads这似乎不是python代码特有的问题。json数据似乎很好,而OAuth令牌授权似乎(至少部分)在工作。但是,当这些放在一起,它停止工作。
发布于 2012-07-17 21:03:03
我终于能算出来了,为什么它不起作用。
我没有正确设置授权令牌的自动化范围。我使用的令牌没有“授权”进行任何修改,使用它的每个操作都失败了,这些操作试图修改某些内容(添加下载)。
我必须将正确的作用域添加到授权中,才能使其工作。
发布于 2012-07-23 22:36:52
我提供了一个关于如何在没有任何身份验证的情况下将JSON数据发布到V3 API的答案,但由于您已经确定了最初的问题在于没有正确设置OAUTH,我想我将提供一个编程解决方案来获得一个令牌(这个实现每次运行脚本时都会得到一个令牌,而在实践中只需要执行一次,并且令牌将被本地存储)。
import urllib2
import json
import getpass
import base64
# Generate a token from the username and password.
# NOTE: this is a naive implementation. Store pre-retrieved tokens if possible.
username = 'pelson'
passwd = getpass.getpass() # <- this just puts a string in passwd (plaintext)
req = urllib2.Request("https://api.github.com/authorizations")
# add the username and password info to the request
base64string = base64.encodestring('%s:%s' % (username, passwd)).replace('\n', '')
req.add_header("Authorization", "Basic %s" % base64string)
data = json.dumps({"scopes":["repo"], "note":"Access to your repository."})
result = urllib2.urlopen(req, data)
result = json.loads('\n'.join(result.readlines()))
token = result['token']一旦您有了这个令牌,它就可以用于任何“回购”作用域操作。让我们将一个新问题添加到存储库中:
# add an issue to the tracker using the new token
repo = 'name_of_repo'
data = json.dumps({'title': 'My automated issue.'})
req = urllib2.Request("https://api.github.com/repos/%s/%s/issues" % (username, repo))
req.add_header("Authorization", "token %s" % token)
result = urllib2.urlopen(req, data)
result = json.loads('\n'.join(result.readlines()))
print result['number']希望这能帮到别人。
发布于 2012-07-23 22:01:15
v3 github有一个很好的特性,它可以将标记转换为html。不需要进行身份验证来请求这些信息,因此在深入研究更棘手的身份验证世界之前,它提供了一个很好的尝试示例。
API文档状态:
呈现一个arbritrary Markdown文档 后/markdown输入 文本所需字符串-要呈现的标记文本
甚至给出一个要转换的减价字符串的示例:
{"text": "Hello world github/linguist#1 **cool**, and #1!"}考虑到这些信息,让我们为该标记的html化版本构建一个urllib2请求。
import urllib2
import json
data = {"text": "Hello world github/linguist#1 **cool**, and #1!"}
json_data = json.dumps(data)
req = urllib2.Request("https://api.github.com/markdown")
result = urllib2.urlopen(req, json_data)
print '\n'.join(result.readlines())结果是一些html表示标记。
在这个例子中,在提交请求时导致404的不是JSON,而是更可能的身份验证(正如您已经回答的那样)。
https://stackoverflow.com/questions/10367859
复制相似问题