[英]google app engine (spring boot) local testing with data store gives Unauthenticated error
Google flexible app engine spring boot project running on local with datastore is giving the com.google.cloud.datastore.DatastoreException
Unauthenticated
exception while saving entity.使用数据存储在本地运行的 Google 灵活应用引擎 Spring Boot 项目在保存实体时给出了
com.google.cloud.datastore.DatastoreException
Unauthenticated
异常。
{
"timestamp": 1512077140003,
"status": 500,
"error": "Internal Server Error",
"exception": "com.google.cloud.datastore.DatastoreException",
"message": "Unauthenticated.",
"path": "/users"
}
The error description here says the request header does not have valid authentication header, But where to place the auth header does not mentioned.这里的错误描述说请求头没有有效的身份验证头,但是没有提到放置身份验证头的位置。
anybody faced same kind of situation?有人面临同样的情况吗?
Canonical Error Code: UNAUTHENTICATED
规范错误代码:
UNAUTHENTICATED
Description: The com.google.cloud.datastore.DatastoreException
simply indicates that the request did not have valid authentication credentials.说明:
com.google.cloud.datastore.DatastoreException
仅表示请求没有有效的身份验证凭据。
Recommended Action: Do not retry without fixing the problem.推荐的操作:不要在未解决问题的情况下重试。 In this case, you need to check the login credentials again.
在这种情况下,您需要再次检查登录凭据。
More read can be found on this page https://cloud.google.com/datastore/docs/concepts/errors#error_codes可以在此页面上找到更多阅读https://cloud.google.com/datastore/docs/concepts/errors#error_codes
Solution:解决方案:
Simply you have to run gcloud beta auth application-default login
.只需运行
gcloud beta auth application-default login
。 But I spent a half of a day until found it there .但是我花了半天时间才在那里找到它。 I thought it should be enough to
gcloud init
.我认为
gcloud init
应该足够了。 As an alternative, you can use google authentication via Google OAuth2 but that is the hard way.作为替代方案,您可以通过 Google OAuth2 使用 google 身份验证,但这很难。
I ended up going in another direction.我最终走向了另一个方向。
application.properties
using the spring.cloud.gcp.datastore.credentials.location=file://FULL_PATH_TO_THE_CREDENTIALS_FILE
and still use a remote Datastore;spring.cloud.gcp.datastore.credentials.location=file://FULL_PATH_TO_THE_CREDENTIALS_FILE
在您的application.properties
引用它,并且仍然使用远程数据存储;src/test/resources/application.properties
# if it exists, comment out or delete the following property:
# spring.cloud.gcp.datastore.credentials.location
# add the following
spring.cloud.gcp.emulator-enabled=true
# if you're not using environment variables, make sure the following property exists
spring.cloud.gcp.project-id=your-project-id
Be aware that if your running integration tests, or mixing remote and local tests, you might need different .properties
files for each test.请注意,如果您正在运行集成测试,或混合远程和本地测试,您可能需要为每个测试使用不同的
.properties
文件。
private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
private static LocalDatastoreHelper helper;
@BeforeClass
public static void setUpClass() throws IOException, InterruptedException {
logger.info("[Datastore-Emulator] start");
helper = LocalDatastoreHelper.create();
helper.start();
logger.info("[Datastore-Emulator] listening on port {}", helper.getPort());
System.setProperty(DatastoreHelper.LOCAL_HOST_ENV_VAR, "localhost:" + helper.getPort());
}
@AfterClass
public static void cleanUpClass() throws InterruptedException, TimeoutException, IOException {
logger.info("[Datastore-Emulator] stop");
helper.stop();
}
@Before
public void init() throws IOException {
logger.info("[Datastore-Emulator] cleaning data");
helper.reset();
}
This approach works great for Repositories ( org.springframework.cloud.gcp.data.datastore.repository.DatastoreRepository
), but if you want to use a Datastore
directly, you can check this .这种方法非常适用于 Repositories (
org.springframework.cloud.gcp.data.datastore.repository.DatastoreRepository
),但如果您想直接使用Datastore
,您可以检查这个。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.