5

服务器从移动应用程序接收一次性授权码。我需要将其转换为 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 将一次性代码交换为访问令牌和刷新令牌吗?

4

3 回答 3

2

这是交换访问令牌的授权码的代码

String authorizationcode=*****;
auth2Operations = googleConnectionFactory.getOAuthOperations();
AccessGrant accessGrant =auth2Operations.exchangeForAccess(authorizationcode,"Your      redirect uri",null);
connection = googleConnectionFactory.createConnection(accessGrant);
Google google=connection.getApi();
于 2014-11-07T07:23:35.190 回答
0

要让它运行,您需要为 Google 请求离线访问权限。大多数情况下,更改只是将查询参数“access_type=offline”添加到但是你得到了那个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());
    }
}
于 2014-11-16T18:01:57.783 回答
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();
于 2016-11-05T05:20:01.113 回答