如何解决 gcloud 中的 'File...does not exist!' 错误(代码 403)

How to troubleshoot 'File...does not exist!' error (code 403) in gcloud

我正在尝试执行 cloud.googe.com 中的一些示例代码,将示例音频文件转换为文本,但我收到一条没有意义的错误消息。

我已经完成了指示的所有步骤:

curl -s -H "Content-Type: application/json" \
    -H "Authorization: Bearer "$(gcloud auth application-default print-access-token) \
    https://speech.googleapis.com/v1/speech:recognize \
    -d @sync-request.json

那么,我没有得到教程页面所说的响应,而是收到了 GCP 帮助页面上没有的错误消息:

ERROR: (gcloud.auth.application-default.print-access-token) File /pathofFile/nameofFile.json (pointed by GOOGLE_APPLICATION_CREDENTIALS environment variable) does not exist!
{
  "error": {
    "code": 403,
    "message": "The request is missing a valid API key.",
    "status": "PERMISSION_DENIED"
  }
}

这个错误显然不是因为我没有设置环境变量,因为错误信息是“(pointed by GOOGLE_APPLICATION_CREDENTIALS environment variable)”,错误信息显示的是正确的正确路径JSON 凭证文件。 JSON 凭证文件肯定在那里;我可以在 finder 中看到它。

这里可能出了什么问题?

编辑:当我尝试运行 gcloud auth application-default login 要么 gcloud auth application-default print-access-token, 我收到错误消息 gcloud: command not found

看来我没有完全安装 Cloud SDK。现在我想知道我需要安装哪些 SDK 组件才能完成此任务。我需要安装以下哪个?

The latest available version is: 246.0.0

┌────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│                                                   Components                                                   │
├──────────────────┬──────────────────────────────────────────────────────┬──────────────────────────┬───────────┤
│      Status      │                         Name                         │            ID            │    Size   │
├──────────────────┼──────────────────────────────────────────────────────┼──────────────────────────┼───────────┤
│ Update Available │ BigQuery Command Line Tool                           │ bq                       │   < 1 MiB │
│ Update Available │ Cloud SDK Core Libraries                             │ core                     │  10.5 MiB │
│ Not Installed    │ App Engine Go Extensions                             │ app-engine-go            │  56.4 MiB │
│ Not Installed    │ Cloud Bigtable Command Line Tool                     │ cbt                      │   6.3 MiB │
│ Not Installed    │ Cloud Bigtable Emulator                              │ bigtable                 │   5.6 MiB │
│ Not Installed    │ Cloud Datalab Command Line Tool                      │ datalab                  │   < 1 MiB │
│ Not Installed    │ Cloud Datastore Emulator                             │ cloud-datastore-emulator │  18.4 MiB │
│ Not Installed    │ Cloud Datastore Emulator (Legacy)                    │ gcd-emulator             │  38.1 MiB │
│ Not Installed    │ Cloud Firestore Emulator                             │ cloud-firestore-emulator │  40.5 MiB │
│ Not Installed    │ Cloud Pub/Sub Emulator                               │ pubsub-emulator          │  34.8 MiB │
│ Not Installed    │ Cloud SQL Proxy                                      │ cloud_sql_proxy          │   3.7 MiB │
│ Not Installed    │ Emulator Reverse Proxy                               │ emulator-reverse-proxy   │  14.5 MiB │
│ Not Installed    │ Google Cloud Build Local Builder                     │ cloud-build-local        │   5.9 MiB │
│ Not Installed    │ Google Container Registry's Docker credential helper │ docker-credential-gcr    │   1.8 MiB │
│ Not Installed    │ gcloud Alpha Commands                                │ alpha                    │   < 1 MiB │
│ Not Installed    │ gcloud Beta Commands                                 │ beta                     │   < 1 MiB │
│ Not Installed    │ gcloud app Java Extensions                           │ app-engine-java          │ 105.6 MiB │
│ Not Installed    │ gcloud app PHP Extensions                            │ app-engine-php           │  21.9 MiB │
│ Not Installed    │ gcloud app Python Extensions                         │ app-engine-python        │   6.0 MiB │
│ Not Installed    │ gcloud app Python Extensions (Extra Libraries)       │ app-engine-python-extras │  28.5 MiB │
│ Not Installed    │ kubectl                                              │ kubectl                  │   < 1 MiB │
│ Installed        │ Cloud Storage Command Line Tool                      │ gsutil                   │   3.8 MiB │
└──────────────────┴──────────────────────────────────────────────────────┴──────────────────────────┴───────────┘
To install or remove components at your current SDK version [245.0.0], run:
  $ gcloud components install COMPONENT_ID
  $ gcloud components remove COMPONENT_ID

编辑: 非常感谢大家的帮助。我最终重新安装了 SDK。在我通过先下载文件然后 运行 安装 SDK 之前 ./google-cloud-sdk/install.sh

但是这次我删除了我电脑上的所有SDK文件并且运行 curl https://sdk.cloud.google.com | bash 除此之外,我确保将 "google-cloud-sdk" 目录保存到我的根目录,这样我就可以使用默认的 .rc 文件路径。

cURL 命令使用 gcloud 工具进行身份验证:gcloud auth application-default print-access-token。由于您使用本地计算机 运行 或提交 API 请求,因此您需要 install and initialize the Cloud SDK (MAC) 在您的计算机中使用 gcloud 工具。