有人可以举一个在github中上传发布资产的python请求示例吗? [英] Can someone give a python requests example of uploading a release asset in github?

查看:79
本文介绍了有人可以举一个在github中上传发布资产的python请求示例吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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'))

这给了我

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

我要去哪里错了?

推荐答案

因此,我将以这样的建议作为开头:如果您使用库,那么就像:

So I'll preface this with the advice that if you use a library it's as easy as:

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'))

请记住,我还将以"application/binary"不是真正的媒体类型作为开头(请参阅:

With that in mind, I'll also preface this with "application/binary" is not a real media type (see: https://www.iana.org/assignments/media-types/media-types.xhtml)

接下来,如果您阅读文档 ,您会注意到GitHub要求客户端具有 real SNI(服务器名称指示),因此,根据您的Python版本,您可能还必须安装pyOpenSSLpyasn1ndg-httpsclient来自PyPI.

Next, if you read the documentation, you'll notice that GitHub requires clients that have real SNI (Server Name Indication), so depending on your version of Python, you may also have to install pyOpenSSL, pyasn1, and ndg-httpsclient from PyPI.

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

I'm not sure what the URL looks like for enterprise instances, but for public GitHub, it looks like:

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

因此,您将把它作为url,另外,您还将需要您的身份验证凭据(在您的情况下,您似乎想使用基本身份验证).然后,您需要在标题中使用 valid 媒体类型,例如

So you're going to have that as url, plus you're going to want your auth credentials (in your case you seem to want to use basic auth). Then you're going to want a valid media-type in the headers, e.g.,

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

您的通话看起来非常正确:

And your call would look pretty much exactly correct:

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

要获取正确的网址,您应该执行以下操作:

To get the correct url, you should do:

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

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

Note this is a URITemplate. You'll need to remove the templating or use a library like uritemplate.py to parse it and use it to build your URL for you.

最后提醒一下,github3.py(原始示例中的库)为您处理了所有这些工作.

One last reminder, github3.py (the library in the original example) takes care of all of this for you.

这篇关于有人可以举一个在github中上传发布资产的python请求示例吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆