我正在尝试使用从 Android 应用程序获得的访问令牌从 google userinfo 端点获取用户信息。我收到以下消息:
GET https://www.googleapis.com/oauth2/v1/userinfo?oauth_token=string
{
"code" : 401,
"errors" : [ {
"domain" : "global",
"message" : "Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.",
"reason" : "unauthorized"
} ],
"message" : "Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.",
"status" : "UNAUTHENTICATED"
}
您可以看到范围是正确的。在我在下一步中提供我的电子邮件地址和密码后,令牌会自行生成。在代码中,我以这种方式使用它。我做错了什么?
public Optional<SocialProfileDto> findByToken(String token) {
Credential credential =
new Credential(BearerToken.authorizationHeaderAccessMethod()).setAccessToken(token);
Oauth2 oauth2 = new Oauth2.Builder(new NetHttpTransport(), new JacksonFactory(), credential).setApplicationName(
"Oauth2").build();
Userinfoplus userinfo = null;
try {
userinfo = oauth2.userinfo().get().setOauthToken(token).execute();
} catch (IOException e) {
e.printStackTrace();
}
try {
userinfo.toPrettyString();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}