com.google.cloud.datastore.DatastoreException:请求缺少必需的身份验证凭据
com.google.cloud.datastore.DatastoreException: Request is missing required authentication credential
我正在使用 google 应用引擎和 google 数据存储。
我正在使用 google 库
com.google.cloud.datastore.Datastore
我的示例代码是:
Datastore datastore = DatastoreOptions.getDefaultInstance().getService();
Key taskKey = datastore.newKeyFactory().setKind(entityName).newKey(id);
//populate some fields....
datastore.put(task);
我正在使用 spring-boot 和 jetty 作为容器。
在本地,它工作正常并且数据在 google 数据存储中更新。
问题是当我将应用程序部署到 google-app-engine 时,当我进入 datastore.put 方法时出现以下异常。
com.google.cloud.datastore.DatastoreException: Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.
com.google.cloud.datastore.spi.v1.HttpDatastoreRpc.translate(HttpDatastoreRpc.java:129)
com.google.cloud.datastore.spi.v1.HttpDatastoreRpc.commit(HttpDatastoreRpc.java:155)
com.google.cloud.datastore.DatastoreImpl.call(DatastoreImpl.java:485)
注意:在本地环境和 google-app-engine 中,我定义了指向 json 文件的环境变量 GOOGLE_APPLICATION_CREDENTIALS,其中包含 google API.
根据 connecting to Datastore from App Engine in Java 的文档,有几个选项可用,因此您可以选择 Objectify(第三方库)、数据存储区 API 或 数据存储区客户端库.
使用客户端库时,您必须知道它们使用 Application Default Credentials,如文档所述,如果环境变量 GOOGLE_APPLICATION_CREDENTIALS
,ADC 使用默认值App Engine 为通过该服务的应用程序 运行 提供的服务帐户。因此,在您的情况下,我认为您不应该定义环境变量,以便 App Engine 使用其默认服务帐户。
如果您仍在为 com.google.cloud.datastore.DatastoreException: Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential.
苦苦挣扎,您可以明确设置凭据,如下例所示:
Resource credentialsCyberpower = resourceLoader.getResource("classpath:yourservice-datastore-access.json");
GoogleCredentials credentials = GoogleCredentials.fromStream(credentialsCyberpower.getInputStream())
.createScoped(Lists.newArrayList("https://www.googleapis.com/auth/cloud-platform"));
DatastoreOptions options =
DatastoreOptions.newBuilder().setProjectId("XXXXXX").setCredentials(credentials).build();
Datastore datastore = options.getService();
ObjectifyService.init(new ObjectifyFactory(datastore));
在 IAM 服务帐户中生成 yourservice-datastore-access.json
。使用 Objectify 6.0.5
如果您将 <url-stream-handler>urlfetch</url-stream-handler>
与 Java8 和 Objectify 6 一起使用,您将必须切换到 native
并启用计费。
我最近遇到了这个问题,花了很多时间来解决这个问题,可以找到更多信息here
我正在使用 google 应用引擎和 google 数据存储。 我正在使用 google 库
com.google.cloud.datastore.Datastore
我的示例代码是:
Datastore datastore = DatastoreOptions.getDefaultInstance().getService();
Key taskKey = datastore.newKeyFactory().setKind(entityName).newKey(id);
//populate some fields....
datastore.put(task);
我正在使用 spring-boot 和 jetty 作为容器。
在本地,它工作正常并且数据在 google 数据存储中更新。
问题是当我将应用程序部署到 google-app-engine 时,当我进入 datastore.put 方法时出现以下异常。
com.google.cloud.datastore.DatastoreException: Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.
com.google.cloud.datastore.spi.v1.HttpDatastoreRpc.translate(HttpDatastoreRpc.java:129)
com.google.cloud.datastore.spi.v1.HttpDatastoreRpc.commit(HttpDatastoreRpc.java:155)
com.google.cloud.datastore.DatastoreImpl.call(DatastoreImpl.java:485)
注意:在本地环境和 google-app-engine 中,我定义了指向 json 文件的环境变量 GOOGLE_APPLICATION_CREDENTIALS,其中包含 google API.
根据 connecting to Datastore from App Engine in Java 的文档,有几个选项可用,因此您可以选择 Objectify(第三方库)、数据存储区 API 或 数据存储区客户端库.
使用客户端库时,您必须知道它们使用 Application Default Credentials,如文档所述,如果环境变量 GOOGLE_APPLICATION_CREDENTIALS
,ADC 使用默认值App Engine 为通过该服务的应用程序 运行 提供的服务帐户。因此,在您的情况下,我认为您不应该定义环境变量,以便 App Engine 使用其默认服务帐户。
如果您仍在为 com.google.cloud.datastore.DatastoreException: Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential.
苦苦挣扎,您可以明确设置凭据,如下例所示:
Resource credentialsCyberpower = resourceLoader.getResource("classpath:yourservice-datastore-access.json");
GoogleCredentials credentials = GoogleCredentials.fromStream(credentialsCyberpower.getInputStream())
.createScoped(Lists.newArrayList("https://www.googleapis.com/auth/cloud-platform"));
DatastoreOptions options =
DatastoreOptions.newBuilder().setProjectId("XXXXXX").setCredentials(credentials).build();
Datastore datastore = options.getService();
ObjectifyService.init(new ObjectifyFactory(datastore));
在 IAM 服务帐户中生成 yourservice-datastore-access.json
。使用 Objectify 6.0.5
如果您将 <url-stream-handler>urlfetch</url-stream-handler>
与 Java8 和 Objectify 6 一起使用,您将必须切换到 native
并启用计费。
我最近遇到了这个问题,花了很多时间来解决这个问题,可以找到更多信息here