【问题标题】:OAuthRequestException received in App Engine Java Servlet在 App Engine Java Servlet 中收到 OAuthRequestException
【发布时间】:2016-05-21 07:56:14
【问题描述】:

我正在尝试访问 Java Servlet 中的 App Engine 用户配置文件,如下所示。

String scope = "https://www.googleapis.com/auth/userinfo.email profile";
OAuthService oathService = OAuthServiceFactory.getOAuthService();
User user = oathService.getCurrentUser(scope);

每当访问用户数据时,都会抛出 OAuthRequestException。相同的代码在开发模式下有效,但在部署模式下无效。

我不明白为什么会抛出错误。据我了解,所需的权限是可用的,如在线文档中所述(我猜!)。

【问题讨论】:

  • 你能发布一个堆栈跟踪吗?
  • @saiyr 因为它只发生在部署模式下,所以我没有堆栈跟踪(让我知道是否可以继续)。可疑的一件事是,一个 url 被映射到这个 servlet,并且将为这个 servlet 操作一个新选项卡。因为它是在开发模式下工作的,所以我没有太多的烦恼。
  • 您应该能够看到或记录它,以便它出现在应用日志中。
  • @saiyr 我在异常处理程序中调用 ex.printStackTrace() 在 Google 开发人员控制台日志查看器中没有看到堆栈跟踪。但是,日志信息消息在日志查看器中可见。
  • 最好直接使用 Logger 并确保 logging.properties 配置正确。

标签: java google-app-engine google-cloud-endpoints google-cloud-platform google-oauth


【解决方案1】:

通过调用UserService 获取当前User 解决了该错误。但是,这不适用于开发模式。可能是 Google App Engine 团队的人可以解释一下!

UserService userService = UserServiceFactory.getUserService();
user = userService.getCurrentUser();

if (user == null) {
    res.sendRedirect(userService.createLoginURL(req.getRequestURI()));
    return;
}

【讨论】:

    【解决方案2】:

    当请求不是有效的 OAuth 请求时,将引发 OAuthRequestException。 这个link有一些例子,例如:

       String scope = "https://www.googleapis.com/auth/userinfo.email";
       try {
        user = oauth.getCurrentUser(scope);
      } catch (OAuthRequestException e) {
        log("Impossible to authenticate the request from the image resizer, oauth request exception: "
            + e.getMessage());
        resp.setStatus(HttpStatusCodes.STATUS_CODE_UNAUTHORIZED);
        return;
      }
    

    【讨论】:

      猜你喜欢
      • 2015-03-05
      • 2014-01-20
      • 2014-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-20
      • 1970-01-01
      相关资源
      最近更新 更多