缺少 urllib 响应 headers
urllib response headers missing
我正在使用 Python3 urllib 尝试使用 the GitHub API.
为工件获取 URL
下面的 curl 工作正常,它(正确地)显示有一个 Location header 和我想要的 URL。没有回复内容:只有headers项:
curl -i -u"$USER:$GH_ACCESS_TOKEN" https://api.github.com/repos/$ORG/$REPO/actions/artifacts/21877858/zip
对于 urllib 代码,auth 正在运行,我可以从其他端点返回 JSON 响应。但是我返回的 headers 不包括 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
curl doesn't follow redirect by default 所以你最终得到一个带有 Location
header.
的 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
我正在使用 Python3 urllib 尝试使用 the GitHub API.
为工件获取 URL下面的 curl 工作正常,它(正确地)显示有一个 Location header 和我想要的 URL。没有回复内容:只有headers项:
curl -i -u"$USER:$GH_ACCESS_TOKEN" https://api.github.com/repos/$ORG/$REPO/actions/artifacts/21877858/zip
对于 urllib 代码,auth 正在运行,我可以从其他端点返回 JSON 响应。但是我返回的 headers 不包括 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
curl doesn't follow redirect by default 所以你最终得到一个带有 Location
header.
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