【问题标题】:Google endpoints OAuth with Post methods带有 Post 方法的 Google 端点 OAuth
【发布时间】:2016-08-18 17:36:14
【问题描述】:

我想将 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 类型方法一起使用?

【问题讨论】:

    标签: java oauth google-cloud-endpoints endpoints


    【解决方案1】:

    就像这样:

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

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

    如果您确实想在这些范围之外传递多个对象,则需要创建一个包装类,其中这两个对象是带有 getter/setter 的属性。

    【讨论】:

    • 谢谢,我刚刚发现 AndroidStudio 的 lint 检查器将其标记为错误,但它编译得很好。看起来像一个已知的错误。
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-27
      • 1970-01-01
      • 2019-11-10
      • 2015-08-28
      • 2011-06-14
      • 1970-01-01
      相关资源
      最近更新 更多