はじめに
gcloud コマンドで key を取得しようとした際、The Application Default Credentials are not available.と出た時の対処法についてまとめます。
原因と対処
default のkeyが読み込めていないことが濃厚です。
対処法としては、環境変数でgcp の key の場所を定義するだけでパスすることができます。
詳細
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
% curl -X POST \ -H "Authorization: Bearer "$(gcloud auth application-default print-access-token) \ -H "Content-Type: application/json; charset=utf-8" \ -d @request.json \ https://translation.googleapis.com/language/translate/v2 ERROR: (gcloud.auth.application-default.print-access-token) The Application Default Credentials are not available. They are available if running in Google Compute Engine. Otherwise, the environment variable GOOGLE_APPLICATION_CREDENTIALS must be defined pointing to a file defining the credentials. See https://developers.google.com/accounts/docs/application-default-credentials for more information. { "error": { "code": 403, "message": "The request is missing a valid API key.", "errors": [ { "message": "The request is missing a valid API key.", "domain": "global", "reason": "forbidden" } ], "status": "PERMISSION_DENIED" } } |
gcloud auth application-default print-access-token
がうまく動いていないようですね。
gcloud auth application-default print-access-token
ERROR: (gcloud.auth.application-default.print-access-token) The Application Default Credentials are not available. They are available if running in Google Compute Engine. Otherwise, the environment variable GOOGLE_APPLICATION_CREDENTIALS must be defined pointing to a file defining the credentials. See https://developers.google.com/accounts/docs/application-default-credentials for more information.
環境変数 GOOGLE_APPLICATION_CREDENTIALS
に gcp の key を登録します。
export GOOGLE_APPLICATION_CREDENTIALS=~/.ssh/gcp.json
再度 gcloud auth application-default print-access-token
を叩くとtoken を取得することができます。