[英]Download asset from private github repo with python
I'm trying to download the latest release from my private github repo.我正在尝试从我的私人 github 存储库下载最新版本。 I've setup a token, I can retrieve the release information, but I can't download the release.我已经设置了一个令牌,我可以检索发布信息,但我无法下载发布。
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)
I keep getting 404 responses.. I've tried to use my token in the headers and also tried to use my login credentials with requests HTTPBasicAuth.. Nothing seems to work..我不断收到 404 响应。我尝试在标头中使用我的令牌,还尝试将我的登录凭据与请求 HTTPBasicAuth 一起使用。似乎没有任何效果。
When I check the accepted headers for the url, I get the following:当我检查 url 的接受标头时,我得到以下信息:
{'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'}
I can see the Authorization header and the Accept header.我可以看到授权 header 和接受 header。 I've tried using github's recommendation on setting the header to 'application/octet-stream', but this doesn't work either.我尝试使用 github 的建议将 header 设置为“application/octet-stream”,但这也不起作用。
Usually, MYTOKEN is used as password:通常,MYTOKEN 用作密码:
r = requests.get(
'https://<username>:MYTOKEN@api.github.com/repos/andrewtquick/releasetest/releases/latest')
As in this example :如本例所示:
url = urljoin(GITHUB_API, 'authorizations')
payload = {}
if note:
payload['note'] = note
res = requests.post(
url,
auth = (username, password),
data = json.dumps(payload),
)
Except you would replace the GitHub account password by a PAT (Personal Access Token) .除非您将 GitHub 帐户密码替换为PAT(个人访问令牌) 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.