简体   繁体   English

Google App Engine Cloud Endpoints userId为null

[英]Google App Engine Cloud Endpoints userId is null

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用以下范围,clientId和受众群体进行了注释:

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. Constants.ANDROID_AUDIENCEConstants.WEB_CLIENT_ID相同。 I am not using a web client, but Google told me to add a web client id. 我没有使用网络客户端,但是Google告诉我添加了网络客户端ID。 Does this client id need to have redirect uris and javascript origins specified? 此客户端ID是否需要指定重定向uri和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. 请帮助我弄清楚这一点。

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();

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

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