Google 使用 Post 方法终结 OAuth

Google endpoints OAuth with Post methods

我想将 OAuth 添加到我的端点后端。正如 "Hello Endpoints Code Walk Through" 指出的那样,具有以下内容就足够了:

@ApiMethod(name = "greetings.authed", path = "hellogreeting/authed")
    public HelloGreeting authedGreeting(User user) {
    HelloGreeting response = new HelloGreeting("hello " + user.getEmail());
    return response;
}

但是如果方法类型是 Post 并且它包含一个主体怎么办?我无法将用户和请求正文传递给该方法。

如何将 OAuth 与包含请求正文的 Post 类型方法一起使用?

两个允许的用户对象 spi.auth.common.Userappengine.api.users.User 不算作资源参数,因为它们是在运行时注入的。您可以自由地将资源添加到具有 User 参数的方法。

就像这样:

@ApiMethod(name = "greetings.authed", path = "hellogreeting/authed")
    public HelloGreeting authedGreeting(User user, MyObject myObject) {
    HelloGreeting response = new HelloGreeting("hello " + user.getEmail());
    return response;
}

User 是一种注入类型,Google App Engine 会像 saiyr 指出的那样为您注入。

如果您确实想在这些范围之外传递多个对象,您将需要创建一个包装器 class,使这两个对象具有 getters/setters 的属性。