在某个目录中,我有一个.tar.gz和一个.jar工件。我想使用请求上传这些资产以用于发布。不幸的是,我无法让它发挥作用。
假设这个.tar.gz文件名为peaches.tar.gz,这就是我试过的:
headers = {'Content-Type': 'application/gzip'}
myAuth = {'MyGithubName', 'myToken'}
requests.post('https://api.github.com/repos/MyGithubName/MyRepo/releases/SomeIDNumber/assets?name=peaches.tar.gz, auth= myAuth, headers= headers, data= open('peaches.tar.gz', 'rb'))
答案 0 :(得分:2)
从Github documentation,要上传资产,您需要upload_url
:
POST https://<upload_url>/repos/:owner/:repo/releases/:id/assets?name=foo.zip
您需要从get release API(列表发布,获取单个版本或获取最新版本)中提取此URL。您可以找到here:
注意:这将返回与端点对应的upload_url键 上传发布资产。此密钥是超媒体资源。
上传网址为URI template,例如:
https://uploads.github.com/repos/bertrandmartel/ustream-dl/releases/8727946/assets{?name,label}
要构建它,您可以使用uritemplate模块&amp;展开name
属性(也称为here)
以下内容将获取最新版本并向其上传peaches.tar.gz
资产(名称为peaches.tar.gz
):
import requests
from uritemplate import URITemplate
repo = 'bertrandmartel/ustream-dl'
access_token = 'YOUR_ACCESS_TOKEN'
r = requests.get('https://api.github.com/repos/{0}/releases/latest'.format(repo))
upload_url = r.json()["upload_url"]
t = URITemplate(upload_url)
asset_url = t.expand(name = 'peaches.tar.gz')
headers = {
'Content-Type': 'application/gzip',
'Authorization': 'Token {0}'.format(access_token)
}
r = requests.post(
asset_url,
headers = headers,
data = open('peaches.tar.gz', 'rb').read()
)
print(r.status_code)
print(r.text)