Google 未创建应用引擎实体

Google app engine entities are not being created

我正在使用 google 端点和 google 应用引擎开发 android 应用程序。我的后端似乎实际上没有做任何事情。似乎没有任何内容保存到数据存储区,因此无法从中检索到任何内容。

这里有一些 Api 方法 我写在端点上但不起作用:

private static String getUserId(User user) {
        String userId = user.getUserId();
        if (userId == null) {
            AppEngineUser appEngineUser = new AppEngineUser(user);
            ofy().save().entity(appEngineUser).now();
            // Begin new session for not using session cache.
            Objectify objectify = ofy().factory().begin();
            AppEngineUser savedUser = objectify.load().key(appEngineUser.getKey()).now();
            userId = savedUser.getUser().getUserId();
        }
        return userId;
    }

    @ApiMethod(name = "saveProfile", path = "profile", httpMethod = ApiMethod.HttpMethod.POST)
    public Profile saveProfile(final User user, final ProfileForm profileForm) throws UnauthorizedException {
        if(user == null) {
            throw new UnauthorizedException("Authorization required.");
        }

        String firstName = profileForm.getFirstName();
        String surname = profileForm.getLastName();
        String userEmail = user.getEmail();
        int year = profileForm.getYear();
        int month = profileForm.getMonth();
        int day = profileForm.getDay();

        Profile profile = ofy().load().key(Key.create(Profile.class, getUserId(user))).now();

        if (profile == null) {
            // the user does not have a profile and is creating one for the first time
            profile = new Profile(getUserId(user), firstName, surname, userEmail, year, month, day);
        } else {
            profile.update(firstName, surname, userEmail, year, month, day);
        }
        ofy().save().entity(profile).now();
        return profile;
    }

    @ApiMethod(name = "getProfile", path = "profile", httpMethod = ApiMethod.HttpMethod.GET)
    public Profile getProfile(User user) throws UnauthorizedException {
        if (user == null) {
            throw new UnauthorizedException("Authentication required.");
        }
        return ofy().load().key(Key.create(Profile.class, getUserId(user))).now();
    }
}

配置文件 class 具有 @Entity 注释并在静态块中使用 objectify 注册,如下所示:

static {
        factory().register(AppEngineUser.class);
        factory().register(Profile.class);
}

userId是GAE通过

生成的
com.google.appengine.api.users.User

并且用户 ID 属性 是带有 @Index 注释的字符串。

我也对 api 资源管理器以及它如何响应这些方法感到困惑。每当我调用 saveProfile api 方法时,都会返回一个配置文件对象,其中包含 0userId"example@example.com" 的电子邮件,尽管我相信这是 运行在本地主机上宁。

我也是 运行宁 api HTTP 浏览器,Google 说 "can cause problems." 这就是为什么没有任何效果的原因。我必须加载不安全的脚本才能使用我的 api,但它可能无法正常工作,因为它是通过 HTTP 而不是 HTTPS 托管的。

由于我对 GAE 的理解存在根本性缺陷,导致无法完全测试我的方法的整个问题,还是由于我 运行 在本地主机上。如果是后者,也许我应该部署到 Appspot,事情可能 运行 更顺利。

如果您有任何额外需要帮助,请尽管提出。

谢谢!

在开发人员控制台中检查您的日志。它记录了您执行的所有 API 方法,并会显示是否有任何错误。

因为你得到 example@example.com 作为用户的电子邮件,这让我相信用户没有被 GAE 注入。这可能是因为您在客户端做错了事(例如 Android)。确保您的 Android 应用程序正确要求使用 Google 登录用户并将这些凭据传递给 Android 中的构建器对象。

如果您通过 api 浏览器执行 API 方法,您需要先以 google 用户身份登录,以便在您的方法中填充该用户对象(我想你已经知道了)。

简而言之,检查您的日志和客户端代码。