我正在尝试这样做:
https://developers.google.com/identity/sign-in/web/backend-auth#calling-the-tokeninfo-endpoint
我使用我的CLIENT_ID复制粘贴示例中的Java代码,但我无法获得比用户ID,电子邮件和电子邮件验证更多的信息。 idTokenString验证确定。还有其他人让这个工作吗?
我在OAuth 2.0 Playground中要求这些:
https://www.googleapis.com/auth/plus.login
https://www.googleapis.com/auth/plus.me
https://www.googleapis.com/auth/userinfo.email
https://www.googleapis.com/auth/userinfo.profile
https://www.googleapis.com/auth/plus.moments.write
https://www.googleapis.com/auth/plus.profile.agerange.read
https://www.googleapis.com/auth/plus.profile.language.read
https://www.googleapis.com/auth/plus.circles.members.read
我猜user.profile只是我需要的那个?
这是我的代码:
GoogleIdTokenVerifier verifier = new GoogleIdTokenVerifier.Builder(transport, jsonFactory)
.setAudience(Arrays.asList(CLIENT_ID))
.setIssuer("accounts.google.com")
.build();
GoogleIdToken idToken = verifier.verify(idTokenString);
System.out.println("SUCCESS!");
System.out.println(idToken);
if (idToken != null) {
GoogleIdToken.Payload payload = idToken.getPayload();
// Print user identifier
String userId = payload.getSubject();
System.out.println("User ID: " + userId);
// Get profile information from payload
String email = payload.getEmail();
boolean emailVerified = payload.getEmailVerified();
String name = (String) payload.get("name");
String pictureUrl = (String) payload.get("picture");
String locale = (String) payload.get("locale");
String familyName = (String) payload.get("family_name");
String givenName = (String) payload.get("given_name");
// Use or store profile information
// ...
System.out.println(email);
System.out.println(emailVerified);
System.out.println(name);
System.out.println(pictureUrl);
System.out.println(locale);
System.out.println(familyName);
System.out.println(givenName);
} else {
System.out.println("Invalid ID token.");
}
} catch (GeneralSecurityException | IOException e) {
System.out.println("ERRRRO! Invalid ID token.");
}
使用:java-api-client 1.20.0
答案 0 :(得分:3)
我今天使用com.google.api-client:google-api-client:1.22.0
但我能够解决它。
尝试从OAuth2游乐场获取ID令牌时,我发现有此请求
POST /oauth2/v4/token HTTP/1.1
Host: www.googleapis.com
Google图书馆TOKEN_SERVER_URL
中的GoogleOAuthConstants
标记为https://accounts.google.com/o/oauth2/token
,其值为public class GoogleAuthorizationCodeTokenV4Request extends GoogleAuthorizationCodeTokenRequest {
public GoogleAuthorizationCodeTokenV4Request(HttpTransport transport, JsonFactory jsonFactory, String clientId, String
clientSecret, String code, String redirectUri) {
super(transport, jsonFactory, "https://www.googleapis.com/oauth2/v4/token", clientId, clientSecret,
code, redirectUri);
}
}
为了解决这个问题,我已经创建了以下课程
GoogleAuthorizationCodeTokenRequest
然后只调用原来的return new GoogleAuthorizationCodeTokenV4Request(new NetHttpTransport(), JacksonFactory.getDefaultInstance(),
clientId, secret, authToken, callBack)
.execute();
profile
使用id_token
范围,所有信息(图片,名称,...)都在{{1}}