Python调用Google Artifact Registry list_tags遇InvalidArgument错误求助
问题
我是编程新手,请多多包涵:) 我已配置好Google凭据,查阅了Artifact Registry(v1)Python库的官方文档,但调用list_tags时仍返回错误:
google.api_core.exceptions.InvalidArgument: 400 Request contains an invalid argument.
我的代码如下:
from google.cloud import artifactregistry_v1 # 使用应用默认凭据最简单 # export GOOGLE_APPLICATION_CREDENTIALS=/path/to/key.json client = artifactregistry_v1.ArtifactRegistryClient() # 最好从环境变量获取 project = "**REDACTED**" location = "europe-west2" repository = "test" parent = f"projects/{project}/locations/{location}/repositories/{repository}" request = artifactregistry_v1.ListTagsRequest(parent=parent) page_result = client.list_tags(request=request) for response in page_result: print(response)
我查阅了官方文档,但找不到问题所在。
编辑
我刚看到ListTagsRequest类的文档说明parent参数应为字符串(我也是这么写的),但PyCharm却高亮提示:
expected type 'Dict', and got 'str' instead
解决方案
直接传参替代构建请求对象
无需手动创建ListTagsRequest,直接把parent参数传给list_tags方法:page_result = client.list_tags(parent=parent)这种方式是Google Cloud Python客户端的常用写法,能绕过类型提示和请求对象构建的潜在问题。
更新Artifact Registry库
执行命令升级到最新版本:pip install --upgrade google-cloud-artifact-registry旧版本库可能存在类型定义不匹配的问题,更新后
ListTagsRequest的参数类型应该会和文档一致。校验parent路径正确性
确认项目ID、区域、仓库名称无拼写错误,且该仓库确实存在于指定区域中,路径格式必须严格遵循projects/{project}/locations/{location}/repositories/{repository}。
内容的提问来源于stack exchange,提问作者sc-leeds
相关产品推荐
相关产品推荐

