繁体   English   中英

无法在 Spring 应用程序中使用 Google 和 Oauth2 从 SSO 中的 UserProfile 信息中获取 email 信息

[英]Not able to get email information from UserProfile info in SSO using Google with Oauth2 in Spring Application

我使用 Google SSO 登录与我的应用程序进行了 SSO 集成。 在 10 月 20 日之前,我能够从 Google UserProfile 获取信息,并且用户能够成功登录到应用程序。

但在 10 月 20 日之后,用户无法使用 SSO 登录,因为我没有从 Google 获得 email 信息。 我不知道 Google Oauth 方面突然发生了什么,我没有收到数据。

我从 Google Developer 管理控制台看到所有 API 都已启用,包括 People、Google+,并且我的应用程序属性中有相同的 client_id 和 Secret 密钥,这些密钥存在于开发人员控制台中。 我试过改变 scope。

目前,我在应用程序中设置的 scope 是 Email scope,我从过去 2 年得到了信息。 我更改为配置文件,将 scope 设置为配置文件,但之后我也只得到名称信息而不是 email 和其他信息。

<dependency>


<groupId>org.springframework.social</groupId>


<artifactId>spring-social-google</artifactId>


<version>1.0.0.RELEASE</version>

</dependency>

代码:

public class GoogleConnectionFactory extends OAuth2ConnectionFactory<Google> {

        public GoogleConnectionFactory(String clientId, String clientSecret) {
            super("google", new GoogleServiceProvider(clientId, clientSecret),
                    new GoogleAdapter());
        }



@Override
        protected String extractProviderUserId(AccessGrant accessGrant) {
            Google api = ((GoogleServiceProvider)getServiceProvider()).getApi(accessGrant.getAccessToken());
            UserProfile userProfile = getApiAdapter().fetchUserProfile(api);
            return userProfile.getUsername();
        }
    }

public Connection<S> createConnection(AccessGrant accessGrant) {

        return new OAuth2Connection<S>(getProviderId(), extractProviderUserId(accessGrant), accessGrant.getAccessToken(),
                accessGrant.getRefreshToken(), accessGrant.getExpireTime(), getOAuth2ServiceProvider(), getApiAdapter());       
}


public Connection getConnection(AccessGrant accessGrant, String providerId){    
            GoogleConnectionFactory connectionFactory = (GoogleConnectionFactory) connectionFactoryLocator.getConnectionFactory(providerId);
            return connectionFactory.createConnection(accessGrant); 
}

public RedirectView getAuthorizeUrl(String providerId, HttpSession session, String returnUrl) {

    String state = UUID.randomUUID().toString();
    session.setAttribute("STATE", state);
    OAuth2Operations oauthOperations = null;
    OAuth2Parameters params = new OAuth2Parameters();
    if(SocialConstants.LINKEDIN_PROVIDER_ID.equals(providerId)){
        LinkedInConnectionFactory connectionFactory = (LinkedInConnectionFactory) connectionFactoryLocator.getConnectionFactory(providerId);
        oauthOperations = connectionFactory.getOAuthOperations();
        params.setRedirectUri(returnUrl);
    }else if(SocialConstants.GOOGLE_PROVIDER_ID.equals(providerId)){
        GoogleConnectionFactory connectionFactory = (GoogleConnectionFactory) connectionFactoryLocator.getConnectionFactory(providerId);
        //ConnectionData cd = new ConnectionData("google", null, null, null, null, "ddd", null, null, null);
        //connectionFactory.createConnection(cd);

        oauthOperations = connectionFactory.getOAuthOperations();
        //params.setScope("profile,email");//profile
        params.setScope("email");
        params.put("prompt", new ArrayList<>(Arrays.asList("consent")));//profile

        params.setRedirectUri(returnUrl);
    }

    params.setState(state);
    String authorizeUrl = oauthOperations.buildAuthorizeUrl(GrantType.AUTHORIZATION_CODE, params);
    return new RedirectView(authorizeUrl);
}
  1. 在获得连接时,我应该从 UserProfile 获取 email。 我正在获取 UserProfile Object 但不是所有信息。

  2. 我从谷歌得到正确的 url 来获取人员数据

  3. 返回 url 也是正确的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM