2016-07-01 17 views
0
url = 'https://github.abc.defcom/api/v3/repos/abc/def/releases/401/assets?name=foo.sh' 
r = requests.post(url, headers={'Content-Type':'application/binary'}, data=open('sometext.txt','r'), auth=('user','password')) 

这是给我有人可以给一个python请求在github上传一个发布资产的例子吗?

>>> r.text 
u'{"message":"Not Found","documentation_url":"https://developer.github.com/enterprise/2.4/v3"}' 

我要去哪里错了?

+0

可能是一个url错误,根据该文档链接也可能是auth错误 – nthall

回答

2

所以我会建议前言本,如果你使用一个库是那么容易,因为:

from github3 import GitHubEnterprise 

gh = GitHubEnterprise(token=my_token) 
repository = gh.repository('abc', 'def') 
release = repository.release(id=401) 
asset = release.upload_asset(content_type='application/binary', name='foo.sh', asset=open('sometext.txt', 'rb')) 

考虑到这一点,我会还与前言本“应用程序/二进制”不一个真正的媒体类型(参见:https://www.iana.org/assignments/media-types/media-types.xhtml

接下来,如果你read the documentation,你会发现,GitHub的要求有真正 SNI(服务器名称指示)客户端,所以根据您的Python版本,你也可以必须安装pyOpenSSL,pyasn1和来自PyPI的ndg-httpsclient

我不知道什么是URL看起来像是企业的实例,但对于公共GitHub上,它看起来像:

https://uploads.github.com/repos/octocat/Hello-World/releases/1/assets?name=foo.sh 

那么你将有一个为url,再加上你会想要你的身份验证凭据(在你的情况下,你似乎想要使用基本身份验证)。然后,你会想在头一个有效媒体类型,例如,

headers = {'Content-Type': 'text/plain'} 

,您的电话看起来几乎完全正确:

requests.post(url, headers=headers, data=open('file.txt', 'rb'), auth=(username, password)) 

为了得到正确的URL,你应该做的:

release = requests.get(release_url, auth=(username, password)) 
upload_url = release.json().get('upload_url') 

注意这是一个URITemplate。您需要删除模板或使用类似uritemplate.py的库来解析它并使用它为您构建网址。

最后一个提示,github3.py(原始示例中的库)为您处理所有这些问题。

相关问题