5

服务器从移动应用程序接收到one-time authorization code。我需要将其转换为spring-social访问令牌并刷新令牌,并将它们保存在服务器数据库中供以后使用。Spring Social Google - 将一次性授权码转换为服务器上的访问令牌/刷新令牌

我当前的代码:

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为访问令牌和刷新令牌交换一次性代码?

回答

2

这是为访问令牌

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

为了得到它去你将需要申请offline access for 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()); 
    } 
} 
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(); 
相关问题