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(); 
相關問題