【问题标题】:OAuth 2.0 authentication error when retrieving Google userinfo in GWT在 GWT 中检索 Google 用户信息时出现 OAuth 2.0 身份验证错误
【发布时间】:2012-11-15 20:31:23
【问题描述】:

我对使用请求进行编程不是很熟悉,所以我将尽量简洁,并尽可能地描述我的问题。

我正在尝试获取可用于获取 Google 帐户的用户信息的身份验证令牌。 我成功获得了这种形式的令牌:https://oauth2-login-demo.appspot.com/oauthcallback#access_token={accesstoken}&token_type=Bearer&expires_in=3600

然后我按照本教程的 TODO#11:http://www.sw-engineering-candies.com/blog-1/howtogetuserinformationwithoauth2inagwtandgoogleappenginejavaapplication

@Override
public LoginInfo loginDetails(final String token) {
    String url = "https://www.googleapis.com/oauth2/v1/userinfo?alt=json&access_token=" 
        + token;

    final StringBuffer r = new StringBuffer();
    try {
        final URL u = new URL(url);
        final URLConnection uc = u.openConnection();
        final int end = 1000;
        InputStreamReader isr = null;
        BufferedReader br = null;
        try {
            isr = new InputStreamReader(uc.getInputStream());
            br = new BufferedReader(isr);
            final int chk = 0;
            while ((url = br.readLine()) != null) {
                if ((chk >= 0) && ((chk < end))) {
                    r.append(url).append('\n');
                }
            }
        } catch (final java.net.ConnectException cex) {
            r.append(cex.getMessage());
        } catch (final Exception ex) {
            log.log(Level.SEVERE, ex.getMessage());
        } finally {
            try {
                br.close();
            } catch (final Exception ex) {
                log.log(Level.SEVERE, ex.getMessage());
            }
        }
    } catch (final Exception e) {
        log.log(Level.SEVERE, e.getMessage());
    }

    final LoginInfo loginInfo = new LoginInfo();
    try {
        final JsonFactory f = new JsonFactory();
        JsonParser jp;
        jp = f.createJsonParser(r.toString());
        jp.nextToken();
        while (jp.nextToken() != JsonToken.END_OBJECT) {
            final String fieldname = jp.getCurrentName();
            jp.nextToken();
            if ("picture".equals(fieldname)) {
                loginInfo.setPictureUrl(jp.getText());
            } else if ("name".equals(fieldname)) {
                loginInfo.setName(jp.getText());
            } else if ("email".equals(fieldname)) {
                loginInfo.setEmailAddress(jp.getText());
            }
        }
    } catch (final JsonParseException e) {
        log.log(Level.SEVERE, e.getMessage());
    } catch (final IOException e) {
        log.log(Level.SEVERE, e.getMessage());
    }
    return loginInfo;
}

当该代码运行时,我发现了一个错误:

WARNING: Authentication error: Unable to respond to any of these challenges: {googlelogin=WWW-Authenticate: GoogleLogin realm="https://www.google.com/accounts/ClientLogin", service="lso"}
{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "required",
    "message": "Login Required",
    "locationType": "header",
    "location": "Authorization"
   }
  ],
  "code": 401,
  "message": "Login Required"
 }
}

我在 GWT 中以开发模式运行。我也尝试部署和运行,但它也发现了一些错误。

一些可能的原因可能是: 1)我的令牌超时(但我刚刚得到它) 2)我必须先部署? 3)我没有正确执行这些步骤? (拿到token后,我必须先验证它?我该怎么做?)

我不太确定如何从这里开始。希望能得到社区的帮助。

【问题讨论】:

    标签: java google-app-engine gwt oauth-2.0


    【解决方案1】:

    这是一个字符串连接错误。该变量没有持有有效的令牌。

    【讨论】:

      猜你喜欢
      • 2018-02-28
      • 1970-01-01
      • 2014-06-07
      • 1970-01-01
      • 2012-05-29
      • 2013-09-28
      • 2016-03-27
      • 2014-08-26
      • 1970-01-01
      相关资源
      最近更新 更多