![](/img/trans.png)
[英]How do I authenticate with my Google Cloud Function when using my Firebase App with Google Sign In?
[英]How do I authenticate my Java application with Google Cloud Storage?
我正在编写一个Java应用程序以与Google Cloud Storage中的文件进行交互。 我找到了要尝试工作的gcloud-java
。
查看他们的示例 ,看来我应该能够在使用gcloud
登录后简单地运行它们,但是似乎没有用。 我正在尝试运行StorageExample
,它说“ 如果未提供 , 将使用登录的项目 ”,但是尽管登录,但无论是否指定,我都无法访问我的项目。
$ gcloud auth login
Your browser has been opened to visit:
https://accounts.google.com/o/oauth2/auth?....
Saved Application Default Credentials.
You are now logged in as [...].
Your current project is [...]. You can change this setting by running:
$ gcloud config set project PROJECT_ID
$ java -cp GCloudDemo.jar com.google.gcloud.examples.storage.StorageExample list
Exception in thread "main" java.lang.IllegalArgumentException: A project ID is required for this service but could not be determined from the builder or the environment. Please set a project ID using the builder.
at com.google.common.base.Preconditions.checkArgument(Preconditions.java:122)
at com.google.gcloud.ServiceOptions.<init>(ServiceOptions.java:317)
...
at com.google.gcloud.examples.storage.StorageExample.main(StorageExample.java:566)
$ java -cp GCloudDemo.jar com.google.gcloud.examples.storage.StorageExample ... list
Exception in thread "main" com.google.gcloud.storage.StorageException: Login Required
at com.google.gcloud.spi.DefaultStorageRpc.translate(DefaultStorageRpc.java:94)
at com.google.gcloud.spi.DefaultStorageRpc.list(DefaultStorageRpc.java:148)
...
at com.google.gcloud.examples.storage.StorageExample$ListAction.run(StorageExample.java:1)
at com.google.gcloud.examples.storage.StorageExample.main(StorageExample.java:579)
Caused by: com.google.api.client.googleapis.json.GoogleJsonResponseException: 401 Unauthorized
{
"code" : 401,
"errors" : [ {
"domain" : "global",
"location" : "Authorization",
"locationType" : "header",
"message" : "Login Required",
"reason" : "required"
} ],
"message" : "Login Required"
}
at com.google.api.client.googleapis.json.GoogleJsonResponseException.from(GoogleJsonResponseException.java:145)
...
at com.google.gcloud.spi.DefaultStorageRpc.list(DefaultStorageRpc.java:138)
... 10 more
显然,我的环境有问题,但是我应该怎么做才能解决? 我想更正我的环境,而不是更改示例。
笔记:
gcloud auth login
无法正常工作的原因是因为Java在Cygwin中的表现不佳,特别是在绝对路径方面。
gcloud
将您的设置和凭据存储在Cygwin主目录~/.config/gcloud
,但是Java会通过System.getProperty("user.home")
在Windows主目录中查找它们。 您可以通过CLOUDSDK_CONFIG
环境变量指定应用程序在哪里寻找该目录,如下所示(确保仅在调用java
时指定它;为您的shell设置它反而会破坏gcloud
):
$ CLOUDSDK_CONFIG=$(cygpath -w ~/.config/gcloud) \
java -cp GCloudDemo.jar com.google.gcloud.examples.storage.StorageExample list
Exception in thread "main" com.google.gcloud.storage.StorageException: Login Required
at ....
请注意,我们不必指定项目,但由于某些原因我们仍未登录。 事实证明,演示应用程序在报告身份验证错误方面做得不好。 在main()
方法的开头添加对AuthCredentials.createApplicationDefaults()
的显式调用会报告一条更有用的错误消息:
Exception in thread "main" java.io.IOException: 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
环境变量。 但是,为什么gcloud
不为我们配置东西? 看起来这实际上是库中的错误 ; 现在,它应该能够找到我们的登录令牌。
同时,我们也可以通过显式指定凭据路径来解决此问题:
$ CLOUDSDK_CONFIG=$(cygpath -w ~/.config/gcloud) \
GOOGLE_APPLICATION_CREDENTIALS=$(cygpath -w ~/.config/gcloud)/application_default_credentials.json \
java -cp GCloudDemo.jar com.google.gcloud.examples.storage.StorageExample list
Bucket{name=...}
成功!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.