从 appengine 网络应用中访问 Google Cloud Storage 时,我在身份验证方面遇到困难。我想使用应用程序默认凭据。也许有人可以给我建议。
我有一个项目,服务帐户:“x@appspot.gserviceaccount.com”。
在 IAM 部分中,项目只有成员 y@gmail.com(这是我的电子邮件地址),角色为所有者。
在项目中,我有两个 Cloud Storage 存储桶:staging.x.appspot.com 和 x.appspot.com。我没有编辑他们的权限。
在该项目中,我有一个用 Java 编写的 appengine Web 应用程序。从它的代码中,我想像这样访问云存储:
import com.google.cloud.AuthCredentials;
import com.google.cloud.storage.Storage;
AuthCredentials credentials=AuthCredentials.createApplicationDefaults();
Storage storage=StorageOptions.builder().authCredentials(credentials).build().service();
具有 Maven 依赖项
<dependency>
<groupId>com.google.appengine</groupId>
<artifactId>appengine-api-1.0-sdk</artifactId>
<version>1.9.38</version>
</dependency>
<dependency>
<groupId>com.google.auth</groupId>
<artifactId>google-auth-library-appengine</artifactId>
<version>0.4.0</version>
</dependency>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>gcloud-java</artifactId>
<version>0.2.3</version>
</dependency>
我启动开发服务器
mvn clean appengine:devserver
并使用
进行部署mvn clean appengine:update
我本以为这就足够了。
但是在一台计算机上的开发服务器中它可以工作,在另一台计算机上我得到异常“401无效身份验证”或类似的东西。我不知道电脑之间有什么区别。
在部署的应用程序中,我收到异常 403 Forbidden。
由以下原因引起:com.google.api.client.googleapis.json.GoogleJsonResponseException:403 { “代码”:403, “错误”:[ { “域”:“全局”, "message": "禁止", “原因”:“禁止” }], "message": "禁止" }
我做错了什么?
最佳答案
正如 Brandon 和 Nick 所说,使用作为所有者的 google 用户(因此不是服务帐户)进行的 gcloud auth login
成功了。感谢您的帮助。
关于java - gcloud : Default Application Credentials in appengine for Cloud Storage (java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37967624/