我对请求编程不是很熟悉,所以我会尽量简洁,我可能会描述我的问题。
我正在尝试获取可用于获取 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)我没有正确执行这些步骤?(拿到令牌后,我必须先验证它?我该怎么做?)
我不太确定如何从这里开始。希望能得到社区的一些帮助。