使用python从私有github存储库下载资产

时间:2020-07-09 17:44:13

标签: python github

我正在尝试从我的私人github存储库下载最新版本。我已经设置了令牌,可以检索发布信息,但是无法下载发布。

import requests
from requests.auth import HTTPBasicAuth

r = requests.get(
    'https://MYTOKEN:@api.github.com/repos/andrewtquick/releasetest/releases/latest')
d = r.json()['assets'][0]["browser_download_url"]

with open('c:/temp/app.exe', 'wb') as f:
    download = requests.get(d, headers={'Authorization': 'MYTOKEN'})
    f.write(download.content)

我不断收到404响应。.我试图在标头中使用令牌,并且还尝试对请求HTTPBasicAuth使用我的登录凭据。.似乎没有任何效果。.

当我检查网址的可接受标头时,得到以下信息:

{'Server': 'GitHub.com',
'Date': 'Thu,
09 Jul 2020 17:34:47 GMT',
'Content-Type': 'application/json; charset=utf-8',
'Transfer-Encoding': 'chunked',
'Status': '200 OK',
'X-RateLimit-Limit': '5000',
'X-RateLimit-Remaining': '4992',
'X-RateLimit-Reset': '1594318601',
'Cache-Control': 'private,
max-age=60,
s-maxage=60',
'Vary': 'Accept,
Authorization,
Cookie,
X-GitHub-OTP,
Accept-Encoding,
Accept,
X-Requested-With',
'ETag': 'W/"44f010aed319d40ab177528a8f41dc78"',
'X-OAuth-Scopes': 'read:packages,
repo',
'X-Accepted-OAuth-Scopes': 'repo',
'X-GitHub-Media-Type': 'github.v3; format=json',
'Access-Control-Expose-Headers': 'ETag,
Link,
Location,
Retry-After,
X-GitHub-OTP,
X-RateLimit-Limit,
X-RateLimit-Remaining,
X-RateLimit-Reset,
X-OAuth-Scopes,
X-Accepted-OAuth-Scopes,
X-Poll-Interval,
X-GitHub-Media-Type,
Deprecation,
Sunset',
'Access-Control-Allow-Origin': '*',
'Strict-Transport-Security': 'max-age=31536000; includeSubdomains; preload',
'X-Frame-Options': 'deny',
'X-Content-Type-Options': 'nosniff',
'X-XSS-Protection': '1; mode=block',
'Referrer-Policy': 'origin-when-cross-origin,
strict-origin-when-cross-origin',
'Content-Security-Policy': "default-src 'none'",
'Content-Encoding': 'gzip',
'X-GitHub-Request-Id': '4E08:54CB:3CB13C:671519:5F075537'}

我可以看到Authorization标头和Accept标头。我尝试使用github的建议将标头设置为“ application / octet-stream”,但这也不起作用。

1 个答案:

答案 0 :(得分:0)

通常,将MYTOKEN用作密码:

DF - Independiente
DF - Racing
GK - Independiente
GK - Racing

here中一样:

 r = requests.get(
'https://<username>:MYTOKEN@api.github.com/repos/andrewtquick/releasetest/releases/latest')

除非您用this example替换GitHub帐户密码。