Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Social Google - converting a one-time authorization code into an access token/ refresh token on the server

The server receives a one-time authorization code from the mobile app. I need to convert this to a spring-social access token and refresh token and save them on the server DB for later usage.

My current 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 is wrong since the ConnectionData is expecting an access token and not the one time authorization code. Any idea how to get spring-social-google to exchange the one-time code for an access token and refresh token?

like image 684
checklist Avatar asked Nov 01 '22 18:11

checklist


1 Answers

This is the code to exchange authorization code for access token

String authorizationcode=*****;
auth2Operations = googleConnectionFactory.getOAuthOperations();
AccessGrant accessGrant =auth2Operations.exchangeForAccess(authorizationcode,"Your      redirect uri",null);
connection = googleConnectionFactory.createConnection(accessGrant);
Google google=connection.getApi();
like image 175
Ruby Kannan Avatar answered Nov 08 '22 05:11

Ruby Kannan