如何使用Google API Explorer使用OAuth测试自己的App Engine端点?

Eli*_*iot 14 google-app-engine oauth-2.0 google-cloud-endpoints

我在App Engine上部署了Endpoints API.我使用Google API资源管理器向不需要登录的API方法发出请求没有问题.我使用的URL是:

https://developers.google.com/apis-explorer/?base=https://[MY_APP_ID].appspot.com/_ah/api

我遇到的问题是调用需要用户登录的API方法,例如:

@ApiMethod(name = "config.get",
        clientIds = {"[MY_CLIENT_ID].apps.googleusercontent.com", "com.google.api.server.spi.Constant.API_EXPLORER_CLIENT_ID"},
        audiences = {"[MY_APP_ID].appspot.com"},
        scopes = {"https://www.googleapis.com/auth/userinfo.email"})
public Config getConfig(User user) throws OAuthRequestException {
    log.fine("user: " + user);

    if (user == null) {
        throw new OAuthRequestException("You must be logged in in order to get config.");
    }

    if (!userService.isUserAdmin()) {
        throw new OAuthRequestException("You must be an App Engine admin in order to get config.");
    }
    ...
Run Code Online (Sandbox Code Playgroud)

在API资源管理器上有一个右上角的开关,当单击它时,允许我指定范围和授权.我只是检查了userinfo.email范围.没什么区别.我从电话中得到的回应是:

503 Service Unavailable

- Show headers -

{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "backendError",
    "message": "java.lang.IllegalStateException: The current user is not logged in."
   }
  ],
  "code": 503,
  "message": "java.lang.IllegalStateException: The current user is not logged in."
 }
}
Run Code Online (Sandbox Code Playgroud)

当Endpoints处于Trusted Tester阶段时,我记得在OAuth2 Playground中有一个手动步骤来获取ID令牌而不是访问令牌或某些此类东西.如果仍然需要,那么现在任何提及它的内容似乎都已从Endpoints文档中消失了,我现在看到了在API Explorer中交换令牌的方法.

Dan*_*oet 12

我看到你有"com.google.api.server.spi.Constant.API_EXPLORER_CLIENT_ID"引号.如果这不是你的Stack Overflow转录中的拼写错误,那就是一个问题.该值已经是一个字符串,因此您只需将文本com.google.api.server.spi.Constant.API_EXPLORER_CLIENT_ID(而不是实际的客户端ID)作为列入白名单的范围传递.那不行.试试这个:

@ApiMethod(name = "config.get",
        clientIds = {"[MY_CLIENT_ID].apps.googleusercontent.com", com.google.api.server.spi.Constant.API_EXPLORER_CLIENT_ID},
        audiences = {"[MY_APP_ID].appspot.com"},
        scopes = {"https://www.googleapis.com/auth/userinfo.email"})
Run Code Online (Sandbox Code Playgroud)

编辑:isUserAdmin在端点中不受支持,可能是导致错误的次要原因.我建议在提供的User对象上提交支持此方法的功能请求(我们可能不会为用户服务本身提供支持,因此它与OAuth登录分开.)