Spring Social Google - 将一次性授权代码转换为服务器上的访问令牌/刷新令牌

时间:2014-08-29 09:15:35

标签: java spring spring-social google-login spring-social-google

服务器从移动应用程序收到one-time authorization code。我需要将其转换为spring-social访问令牌并刷新令牌并将其保存在服务器数据库中以供以后使用。

我目前的代码:

String oneTimeAuthorizationCode= "xxx"; // provided by mobile client

ConnectionData cd = new ConnectionData("google", null, null, null, null, oneTimeAuthorizationCode, null, null, null);
GoogleConnectionFactory googleConnectionFactory = (GoogleConnectionFactory) connectionFactoryLocator.getConnectionFactory("google");
Connection<Google> connection = googleConnectionFactory.createConnection(cd);

// get the google API and work with it
Google  google = (Google) connection.getApi();

oneTimeAuthorizationCode是错误的,因为ConnectionData期望访问令牌而不是一次授权代码。知道如何让spring-social-google交换访问令牌的一次性代码并刷新令牌吗?

3 个答案:

答案 0 :(得分:2)

这是用于交换访问令牌

的授权码的代码
String authorizationcode=*****;
auth2Operations = googleConnectionFactory.getOAuthOperations();
AccessGrant accessGrant =auth2Operations.exchangeForAccess(authorizationcode,"Your      redirect uri",null);
connection = googleConnectionFactory.createConnection(accessGrant);
Google google=connection.getApi();

答案 1 :(得分:0)

要实现这一目标,您需要申请offline access for Google。大多数情况下,更改只是添加查询参数&#39; access_type = offline&#39;但是你得到了oneTimeAuthorizationCode。然后,您将在授权后获得刷新令牌。

对于我自己的项目,我最终自定义ProviderSignInController以手动添加查询参数,因为它不允许您通过REST传递它:

@RequestMapping(value="/{providerId}", method=RequestMethod.POST)
public RedirectView signIn(@PathVariable String providerId, NativeWebRequest request) {
    ConnectionFactory<?> connectionFactory = connectionFactoryLocator.getConnectionFactory(providerId);
    MultiValueMap<String, String> parameters = new LinkedMultiValueMap<String, String>(); 
    preSignIn(connectionFactory, parameters, request);

    // Request offline access for Google+. Will allow a refreshToken
    parameters.put("access_type", Arrays.asList("offline"));

    try {
        return new RedirectView(connectSupport.buildOAuthUrl(connectionFactory, request, parameters));
    } catch (Exception e) {
        logger.error("Exception while building authorization URL: ", e);
        return redirect(URIBuilder.fromUri(signInUrl).queryParam("error", "provider").build().toString());
    }
}

答案 2 :(得分:0)

解决方案:

        GoogleConnectionFactory connectionFactory = new GoogleConnectionFactory("clientId","clientSecret");

        OAuth2Operations oauthOperations = connectionFactory.getOAuthOperations();

        MultiValueMap<String, String> parameters = new LinkedMultiValueMap<String, String>();

        parameters.put("grant_type", Arrays.asList("authorization_code"));

        //"authCodeFromAndroid" to be replaced by the authCode sent from Android, and exactly returned from the method "getServerAuthCode()"
        AccessGrant accessGrant = oauthOperations.exchangeForAccess("authCodeFromAndroid", "", parameters);

        Connection<Google> connection = googleConnectionFactory.createConnection(accessGrant);

        //Then you can continue with the ordinary "connection" as usual
        String providerId = connection.getKey().getProviderId();
        String providerUserId = connection.getKey().getProviderUserId();