我正在使用Python3 urllib尝试使用the GitHub API获取工件的URL。
以下curl正常工作,它(正确)显示存在带有所需URL的Location标头。没有响应内容:只有标题很重要:
curl -i -u"$USER:$GH_ACCESS_TOKEN" https://api.github.com/repos/$ORG/$REPO/actions/artifacts/21877858/zip
对于urllib代码,auth正在工作,我可以从其他端点返回JSON响应。但是我得到的标头不包含Location
。
opener = get_opener() # bunch of boilerplate
req = request.Request(uri)
# same exact headers as curl sends.
# It doesn't change anything if I change `Accept` to "application/vnd.github.v3+json"
# like GH recommends https://docs.github.com/en/free-pro-team@latest/rest/reference/actions#download-an-artifact
req.add_header("Accept", "*/*")
# next line doesn't make a difference
req.add_header("User-Agent", "curl/7.64")
with opener.open(req) as response:
print(response.code, response.headers["Location"])
# prints (200, None)
# If I stringify the headers there is a completely different set than
# what curl shows
答案 0 :(得分:1)
curl不是follow redirect by default,所以您最终得到带有Location
标头的302状态代码。
urllib确实会自动遵循重定向,但是您可以使用one of these solutions,例如:
import urllib.request
githubToken = "YOUR_GITHUB_TOKEN"
url = "https://api.github.com/repos/OWNER/REPO/actions/artifacts/22063356/zip"
class NoRedirection(urllib.request.HTTPErrorProcessor):
def http_response(self, request, response):
return response
https_response = http_response
opener = urllib.request.build_opener(NoRedirection)
req = urllib.request.Request(url, None, headers = {
'Authorization' :f'Token {githubToken}'
})
with opener.open(req) as response:
print(response.code, response.headers["Location"])
输出
302 https://pipelines.actions.githubusercontent.com/NSAmkKgDUSbnHdte1rYFxmKxUVJuvcQLNt6gV7000UAFVCxMSK/_apis/pipelines/1/runs/1/signedartifactscontent?artifactName=my-artifact&urlExpires=2020-10-17T20%3A49%3A00.1948907Z&urlSigningMethod=HMACV1&urlSignature=Nq0YuQKd%2FP4jzyzGklELjfzDtBO04c7HsMgJ%2B1%2Bu%2FWY%3D