Google App Engine云端点userId为空

时间:2021-10-06 15:50:13

I am using a (little modified) workaround from this course, to fetch the userId, which is null if the request was sent from an Android client.

我正在使用本课程的一个(稍加修改的)解决方法来获取userId,如果请求是从Android客户端发送的,则为null。

/**
 * This is an ugly workaround for null userId for Android clients.
 *
 * @param user A User object injected by the cloud endpoints.
 * @return the App Engine userId for the user.
 */
private static String getUserId(User user) {
    String userId = user.getUserId();
    if (userId == null) {
        LOG.info("userId is null, so trying to obtain it from the datastore.");
        AppEngineUser appEngineUser = new AppEngineUser(user);
        ofy().save().entity(appEngineUser).now();

        AppEngineUser savedUser = ofy().load().key(appEngineUser.getKey()).now();
        userId = savedUser.getUser().getUserId();
        LOG.info("Obtained the userId: " + userId);
    }
    return userId;
}

Although I am not able to get the userId.

虽然我无法获得userId。

INFO: Obtained the userId: null

This workaround has already worked perfectly in other projects, so the problem must be elsewhere. My endpoints api is annotated with the following scopes, clientIds and audiences:

这种解决方法已经在其他项目中完美运行,因此问题必须在其他地方。我的端点api注释了以下范围,clientIds和audiences:

scopes = {
            Constants.EMAIL_SCOPE
    },
    clientIds = {
            Constants.API_EXPLORER_CLIENT_ID,
            Constants.WEB_CLIENT_ID,
            Constants.ANDROID_CLIENT_ID
    },
    audiences = {
            Constants.ANDROID_AUDIENCE
    }

Constants.ANDROID_AUDIENCE and Constants.WEB_CLIENT_ID are the same. I am not using a web client, but Google told me to add a web client id. Does this client id need to have redirect uris and javascript origins specified?

Constants.ANDROID_AUDIENCE和Constants.WEB_CLIENT_ID是相同的。我没有使用网络客户端,但Google告诉我要添加一个网络客户端ID。此客户端ID是否需要重定向uris和指定的javascript源?

In my Android client I am using the following to specify the audience.

在我的Android客户端中,我使用以下内容来指定受众。

mCredential = GoogleAccountCredential.usingAudience(
        EndpointService.this,
        "server:client_id:IDIDIDID.apps.googleusercontent.com"
);

Please help me to figure this one out.

请帮我解决这个问题。

1 个解决方案

#1


0  

I just understood why this workaround works. I need to begin a new objectify session so the cache is not used and the userId can be populated.

我只是理解为什么这个解决方法有效。我需要开始一个新的客观化会话,因此不使用缓存并且可以填充userId。

Objectify objectify = ofy().factory().begin();
AppEngineUser savedUser = objectify.load();

#1


0  

I just understood why this workaround works. I need to begin a new objectify session so the cache is not used and the userId can be populated.

我只是理解为什么这个解决方法有效。我需要开始一个新的客观化会话,因此不使用缓存并且可以填充userId。

Objectify objectify = ofy().factory().begin();
AppEngineUser savedUser = objectify.load();