我對編程的要求不是很熟悉,所以我會盡量簡潔,並且可能會描述我的問題。在GWT中檢索Google用戶信息時出現OAuth 2.0身份驗證錯誤
我想獲得一個身份驗證令牌,我可以使用它來獲取Google帳戶的用戶信息。 我成功地得到令牌以這種形式:https://oauth2-login-demo.appspot.com/oauthcallback#access_token= {的accessToken} & token_type =承載& 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)我沒有正確地執行這些步驟? (在得到令牌後,我必須先驗證它?我該怎麼做?)
我不太確定如何從這裏開始。希望得到社區的幫助。