gpt4 book ai didi

google-app-engine - 如何使用 Google API Explorer 通过 OAuth 测试我自己的 App Engine 端点?

转载 作者:太空宇宙 更新时间:2023-11-03 15:18:32 24 4
gpt4 key购买 nike

我在 App Engine 上部署了 Endpoints API。我可以使用 Google API Explorer 向不需要登录的 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.");
}
...

在 API Explorer 上,右上角有一个开关,单击该开关后,我可以指定范围和授权。我只是在检查 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."
}
}

当 Endpoints 处于受信任的测试人员阶段时,我记得在 OAuth2 Playground 中有一个手动步骤来获取 ID token 而不是访问 token 或类似的东西。如果仍然需要,那么现在 Endpoints 文档中似乎已经消失了,我现在也看到了在 API Explorer 中换出 token 的方法。

最佳答案

我看到你有 "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"})

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

关于google-app-engine - 如何使用 Google API Explorer 通过 OAuth 测试我自己的 App Engine 端点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16132727/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com