microsoft / microsoft/azure-devops-python-api
build_client.get_artifact_content_zip() raises "The user '' is not authorized
還沒有人認領這個 Issue。
- 主要語言
- Python
- 星號
- 684
- 分支
- 218
- 平均合併
- 8 天 10 小時
- 30 天內合併 PR
- 1
描述
I opened the issue originally at https://developercommunity.visualstudio.com/content/problem/917755/get-artifact-content-zip-always-returns-authorizat.html
I got "TF400813: The user '' is not authorized to access this resource.", when using build_client.get_artifact_content_zip()
I did find that I can only reproduce it on certain artifacts, not all of them. My suspicion is that for artifacts whose downloadUrl points to some other host, the credential doesn't transfer correctly.
Here's the code:
import argparse
import pathlib
import sys
import tempfile
import zipfile
import azure.devops.connection as azdo_connection
import msrest
# NOTE, please run this with python >= 3.6, in venv that has these reqs:
# * azure-devops~=5.1.0b3
# * msrest~=0.6.9
def parser():
parser = argparse.ArgumentParser(description="Download artifact")
parser.add_argument(
"--url", required=True, help="Example: https://dev.azure.com/MY_ORGANIZATION"
)
parser.add_argument("--access-token", required=True, help="Access token to use.")
parser.add_argument("--project", required=True, help="Azdo project.")
parser.add_argument("--build-id", required=True, help="Current build id.")
parser.add_argument("--artifact-name", required=True, help="Artifact name.")
parser.add_argument(
"--output-dir", default=".", type=pathlib.Path, help="Artifact name."
)
return parser
def main(argv=None):
if argv is None:
argv = sys.argv
args = parser().parse_args(argv[1:])
credentials = msrest.authentication.BasicAuthentication("", args.access_token)
connection = azdo_connection.Connection(base_url=args.url, creds=credentials)
build_client = connection.clients_v5_1.get_build_client()
artifact_info = build_client.get_artifact(
args.project, args.build_id, args.artifact_name
)
print(f"Artifact info: {artifact_info}")
with tempfile.TemporaryFile() as f:
for i in build_client.get_artifact_content_zip(
args.project, args.build_id, args.artifact_name
):
f.write(i)
f.seek(0)
zf = zipfile.ZipFile(f)
outdir = args.output_dir
outdir.mkdir(parents=True, exist_ok=True)
zf.extractall(path=outdir)
if __name__ == "__main__":
sys.exit(main())
A workaround I found is to apply this patch:
@@ -40,9 +42,18 @@ def main(argv=None):
)
print(f"Artifact info: {artifact_info}")
with tempfile.TemporaryFile() as f:
- for i in build_client.get_artifact_content_zip(
- args.project, args.build_id, args.artifact_name
- ):
+ try:
+ stream = build_client.get_artifact_content_zip(
+ args.project, args.build_id, args.artifact_name
+ )
+ except AzureDevOpsServiceError as e:
+ if "The user '' is not authorized to access this resource" in e.message:
+ stream = requests.get(
+ artifact_info.resource.download_url, auth=("", args.access_token)
+ )
+ else:
+ raise
+ for i in stream:
f.write(i)
f.seek(0)
zf = zipfile.ZipFile(f)
貢獻指南
這個儲存庫沒有索引到貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
研究方向
從提供的 Python 重現程式碼和進入點 build_client.get_artifact_content_zip() 開始。針對託管在其他網域上的 artifact,將其驗證行為與 artifact_info.resource.download_url 進行比較,並將回報的授權錯誤作為失敗訊號。完成的標準是:artifact ZIP 下載對相同主機上的下載 URL 和外部下載 URL 都能正常運作,且不需要呼叫方提供 workaround。
由索引模型根據 Issue 內容生成。
評估
- 技術堆疊
- azure, python
- 領域
- api
- Issue 類型
- 缺陷
- 難度
- 4/5
- 預估耗時
- 3-5 天
- 活躍度
- 停滯
- 描述清晰度
- 基本清楚
- 新手友好度
- 38/100