使用 IntelliJ IDEA 调试时如何将服务帐户凭据添加到 Google App Engine?
How to add service account credentials to Google App Engine when debugging with IntelliJ IDEA?
我正在开发一个 Google App Engine(标准环境)应用程序,它使用 Google 云存储。我用过App Engine APIs for Cloud Storage until now, which provides a local emulation for the Cloud Storage using Datastore. As those APIs are getting obsolete now, I have decided to use the recommended APIs, however I am struggling with the credentials when running on the Local Server (I am already using the new Cloud Code plugin, not the old App Engine一个)。
我创建了一个服务帐户,并为其创建并下载了密钥。如果我是 运行 一个普通的 Java 应用程序,我将能够为 VM 指定环境变量,并且我可以提供必要的 -DGOOGLE_APPLICATION_CREDENTIALS=xxxxx.json
参数。 Cloud Code提供的服务器好像没有办法提供环境变量,我只能提供VM选项,所以我不知道如何给它提供必要的环境,或者如何传递凭证给它以其他方式。我让它工作的唯一方法是使用
gcloud auth application-default login
已在 D:\Users\xxxx\AppData\Roaming\gcloud\application_default_credentials.json
中保存凭据。这行得通,但是每当我调试我的应用程序时,我都会收到以下警告:
com.google.auth.oauth2.DefaultCredentialsProvider warnAboutProblematicCredentials
WARNING: Your application has authenticated using end user credentials from Google Cloud SDK. We recommend that most server applications use service accounts instead. If your application continues to use end user credentials from Cloud SDK, you might receive a "quota exceeded" or "API not enabled" error.
我不确定这个警告有多严重,但我听起来确实很害怕。
在我的应用程序中,我使用此代码(Scala,Java 会非常相似)使用凭据创建服务:
val credentials = GoogleCredentials.getApplicationDefault
val storage = StorageOptions.newBuilder().setCredentials(credentials).build().getService
在本地 Google App Engine 服务器上 运行 时传递服务帐户凭据的正确方法是什么?
大警告的问题是 Google 不希望您使用用户凭据代替服务帐户凭据。 Google 正在锁定(限制)scopes/data 第三方服务(您)可以请求的内容。我的建议是不要再使用用户凭据,因为它们最终将不再有效。
有几种方法可以解决这个问题。
方法 1:设置 CLI 以使用服务帐户:
gcloud auth activate-service-account test@development.iam.gserviceaccount.com --key-file=test_google_account.json
为服务帐户使用正确的电子邮件地址。这可以在 Google 云控制台和 JSON 文件中找到。 Google 图书馆将找到这些凭据。在我的网站上,我写了几篇关于服务帐户、应用程序默认凭证 (ADC) 等详细信息的文章
方法二:在代码中指定服务账号
credentials = GoogleCredentials.fromStream(new FileInputStream(service_account_filename))
创建一个标志或环境变量,以便您的代码可以 if-else 决定何时 运行 在您的桌面上处理凭据。
方法三:
如果设置了系统(不是 VM 命令行)环境变量 GOOGLE_APPLICATION_CREDENTIALS
,库将使用该变量指向的文件名作为服务帐户凭据。此文件是 Google 云服务帐户凭据文件,格式为 JSON。
在启动 IntelliJ 之前设置此环境变量。
我的文档链接:
我正在开发一个 Google App Engine(标准环境)应用程序,它使用 Google 云存储。我用过App Engine APIs for Cloud Storage until now, which provides a local emulation for the Cloud Storage using Datastore. As those APIs are getting obsolete now, I have decided to use the recommended APIs, however I am struggling with the credentials when running on the Local Server (I am already using the new Cloud Code plugin, not the old App Engine一个)。
我创建了一个服务帐户,并为其创建并下载了密钥。如果我是 运行 一个普通的 Java 应用程序,我将能够为 VM 指定环境变量,并且我可以提供必要的 -DGOOGLE_APPLICATION_CREDENTIALS=xxxxx.json
参数。 Cloud Code提供的服务器好像没有办法提供环境变量,我只能提供VM选项,所以我不知道如何给它提供必要的环境,或者如何传递凭证给它以其他方式。我让它工作的唯一方法是使用
gcloud auth application-default login
已在 D:\Users\xxxx\AppData\Roaming\gcloud\application_default_credentials.json
中保存凭据。这行得通,但是每当我调试我的应用程序时,我都会收到以下警告:
com.google.auth.oauth2.DefaultCredentialsProvider warnAboutProblematicCredentials
WARNING: Your application has authenticated using end user credentials from Google Cloud SDK. We recommend that most server applications use service accounts instead. If your application continues to use end user credentials from Cloud SDK, you might receive a "quota exceeded" or "API not enabled" error.
我不确定这个警告有多严重,但我听起来确实很害怕。
在我的应用程序中,我使用此代码(Scala,Java 会非常相似)使用凭据创建服务:
val credentials = GoogleCredentials.getApplicationDefault
val storage = StorageOptions.newBuilder().setCredentials(credentials).build().getService
在本地 Google App Engine 服务器上 运行 时传递服务帐户凭据的正确方法是什么?
大警告的问题是 Google 不希望您使用用户凭据代替服务帐户凭据。 Google 正在锁定(限制)scopes/data 第三方服务(您)可以请求的内容。我的建议是不要再使用用户凭据,因为它们最终将不再有效。
有几种方法可以解决这个问题。
方法 1:设置 CLI 以使用服务帐户:
gcloud auth activate-service-account test@development.iam.gserviceaccount.com --key-file=test_google_account.json
为服务帐户使用正确的电子邮件地址。这可以在 Google 云控制台和 JSON 文件中找到。 Google 图书馆将找到这些凭据。在我的网站上,我写了几篇关于服务帐户、应用程序默认凭证 (ADC) 等详细信息的文章
方法二:在代码中指定服务账号
credentials = GoogleCredentials.fromStream(new FileInputStream(service_account_filename))
创建一个标志或环境变量,以便您的代码可以 if-else 决定何时 运行 在您的桌面上处理凭据。
方法三:
如果设置了系统(不是 VM 命令行)环境变量 GOOGLE_APPLICATION_CREDENTIALS
,库将使用该变量指向的文件名作为服务帐户凭据。此文件是 Google 云服务帐户凭据文件,格式为 JSON。
在启动 IntelliJ 之前设置此环境变量。
我的文档链接: