GCPを使うにあたって環境変数設定でエラーが発生
GCPを使うにあたって設定する環境変数「GOOGLE_APPLICATION_CREDENTIALS
」の設定でエラーが発生した
その際に行った解決方法の走り書き程度のメモ
※実行環境はWindows10です
環境変数「GOOGLE_APPLICATION_CREDENTIALS」の設定
Windows10なのでコマンドプロンプト
set GOOGLE_APPLICATION_CREDENTIALS=C:\tools\gcp\my-key.json
or
Powershell から実行
$env:GOOGLE_APPLICATION_CREDENTIALS="C:\tools\gcp\my-key.json"
環境変数の設定後にGCPに接続する処理の実行すると
例外がスローされました: 'System.InvalidOperationException' (mscorlib.dll の中)
Error cannot dispatch message:System.InvalidOperationException: 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.Apis.Auth.OAuth2.DefaultCredentialProvider.<CreateDefaultCredentialAsync>d__11.MoveNext() 場所 C:\Apiary\2020-11-11.14-48-36\Src\Support\Google.Apis.Auth\OAuth2\DefaultCredentialProvider.cs:行 142
--- 直前に例外がスローされた場所からのスタック トレースの終わり ---
というエラーが発生
解決時の方法
PowerShellからgcloud auth application-default login
コマンドを実行するとブラウザでログイン認証しろと要求されるので従う
PS C:\Users\sola> gcloud auth application-default login
The environment variable [GOOGLE_APPLICATION_CREDENTIALS] is set to:
[C:\tools\gcp\my-key.json]
Credentials will still be generated to the default location:
[C:\Users\sola\AppData\Roaming\gcloud\application_default_credentials.json]
To use these credentials, unset this environment variable before
running your application.
Do you want to continue (Y/n)? Y
Your browser has been opened to visit:
https://accounts.google.com/o/oauth2/auth?client_id=******************************************
Credentials saved to file: [C:\Users\sola\AppData\Roaming\gcloud\application_default_credentials.json]
These credentials will be used by any library that requests Application Default Credentials (ADC).
C:\Users\sola\AppData\Local\Google\Cloud SDK\google-cloud-sdk\bin\..\lib\third_party\google\auth\_default.py:69: UserWarning: Your application has authenticated using end user credentials from Google Cloud SDK without a quota project. You might receive a "quota exceeded" or "API not enabled" error. We recommend you rerun `gcloud auth application-default login` and make sure a quota project is added. Or you can use service accounts instead. For more information about service accounts, see https://cloud.google.com/docs/authentication/
warnings.warn(_CLOUD_SDK_CREDENTIALS_WARNING)
Quota project "test-pj" was added to ADC which can be used by Google client libraries for billing and quota. Note that some services may still bill the project owning the resource.
Updates are available for some Cloud SDK components. To install them,
please run:
$ gcloud components update
これで上記のエラーが発生しなくなった