java - Google App Engine Cloud Endpoints userId 为空

标签 java android google-app-engine google-cloud-endpoints

我正在使用来自此 course 的(稍作修改的)解决方法, 以获取 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;
}

虽然我获取不到userId。

INFO: Obtained the userId: null

这个解决方法已经在其他项目中完美运行,所以问题一定出在其他地方。我的端点 api 注释了以下范围、clientIds 和受众:

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

Constants.ANDROID_AUDIENCEConstants.WEB_CLIENT_ID 是一样的。我没有使用网络客户端,而是 Google told me添加网络客户端 ID。此客户端 ID 是否需要指定重定向 URI 和 JavaScript 来源?

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

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

请帮我解决这个问题。

最佳答案

我刚刚明白为什么这个解决方法有效。我需要开始一个新的 objectify session ,以便不使用缓存并且可以填充 userId。

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

关于java - Google App Engine Cloud Endpoints userId 为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26802603/

相关文章:

java - 带有区域设置的日期格式

java - 如何在 log4j html 报告中包含日期和时间列

适用于特定设备的 Android 模拟器

java - 在不更改模型的情况下对数据模型进行编码/解码的最佳方法?

java - 如何捕获缺少 JSP 文件的异常

java - 如何使用 main.xml 中定义的布局添加按钮?

java - Libgdx 音乐未正确处理

java - Google App Engine 示例 - 错误(Java 版本问题?)

google-app-engine - 批量重命名 Google Cloud Storage 上的对象

java - Java 初始化线程时出现拒绝访问错误