从GoogleIdToken获取用户配置文件



我正在尝试这样做:

https://developers.google.com/identity/sign-in/web/backend-auth#calling-标记终点

我用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.");
    }

使用:javaapi客户端1.20.0

我今天在使用com.google.api-client:google-api-client:1.22.0 时遇到了同样的问题

但我解决了它。

问题

当试图从OAuth2游乐场获取id令牌时,我注意到有这个请求

POST /oauth2/v4/token HTTP/1.1 Host: www.googleapis.com

Google库在GoogleOAuthConstants中对TOKEN_SERVER_URL进行了硬编码,值为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

相关内容

  • 没有找到相关文章

最新更新