5

Der Server erhält eine one-time authorization code von der mobilen App. Ich muss dies in ein Spring-Social-Access-Token konvertieren und Token aktualisieren und sie für spätere Verwendung auf der Server-DB speichern.Spring Social Google - Umwandlung eines einmaligen Autorisierungscodes in ein Zugriffstoken/Aktualisierungstoken auf dem Server

Mein aktueller Code:

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 falsch ist, da die connection ein Zugriffstoken erwartet und nicht den Berechtigungscode Zeit. Irgendeine Idee, wie Spring-Social-google den Einmal-Code für ein Access-Token und ein Refresh-Token austauschen kann?

Antwort

2

Dies ist der Code Autorisierungscode für Zugriffstoken

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

Um es geht Sie gehen zu müssen, auszutauschen offline access for Google zu beantragen. Meistens fügt die Änderung nur den Abfrageparameter 'access_type = offline' hinzu, wie auch immer Sie diesen oneTimeAuthorizationCode erhalten haben. Dann erhalten Sie nach der Autorisierung ein Aktualisierungstoken zurück.

Für mein eigenes Projekt, landete ich ProviderSignInController Customizing bis die Abfrage param manuell hinzufügen, da es Ihnen in über REST passieren nicht erlaubt:

@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

Die Lösung:

 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();