GCP 服务使用接口返回 404



我正在尝试使用 GCP 服务使用情况 API 获取项目已启用 API 的列表。

我的 Go 代码如下:

apiService, err := serviceusage.NewService(ctx)
if err != nil {
return fmt.Errorf("error initializing the service usage API: %w", err)
}
apiList, err := apiService.Services.List(project.ProjectId).Do()
if err != nil {
return fmt.Errorf("error getting list of enabled services for %s: %w", project.ProjectId, err)
}

我已确认我拥有必要的应用程序默认凭据,因为我可以访问其他 GCP API。我还确认我为我的项目启用了 serviceusage.googleapis.com API。

但是,我得到一个 404,好像这个 API 端点不再存在:

error getting list of enabled services for scratch-12345: googleapi: got HTTP response code 404 with body: <!DOCTYPE html>
<html lang=en>
<meta charset=utf-8>
<meta name=viewport content="initial-scale=1, minimum-scale=1, width=device-width">
<title>Error 404 (Not Found)!!1</title>
[...]
<a href=//www.google.com/><span id=logo aria-label=Google></span></a>
<p><b>404.</b> <ins>That’s an error.</ins>
<p>The requested URL <code>/v1/scratch-12345/services?alt=json&amp;prettyPrint=false</code> was not found on this server.  <ins>That’s all we know.</ins>

我已经检查了"scratch-12345"是正确的项目 ID,并且是我拥有的项目。

对于此 API,您需要通过字符串"project/PROJECT_NUMBER"而不是项目 ID 来标识项目。以下行可解决此问题:

projNum := fmt.Sprintf("projects/%d", project.ProjectNumber)
apiList, err := apiService.Services.List(projNum).Do()

最新更新