简体   繁体   English

带有数据存储的谷歌应用引擎(spring boot)本地测试给出了未经身份验证的错误

[英]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.我最终走向了另一个方向。

  1. You can authenticate and use a remote Datastore, like Amar mentioned ;您可以验证和使用远程数据存储,就像Amar 提到的那样
  2. You can also get credentials, store it locally, reference it in your 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引用它,并且仍然使用远程数据存储;
  3. But my favorite option is to run it full local using a Datastore emulator, so no authentication is needed at all:但我最喜欢的选择是使用 Datastore 模拟器在本地完全运行它,因此根本不需要身份验证:

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文件。

Your test class你的测试班

    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 ,您可以检查这个

Refs参考资料

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM