5

Máy chủ nhận được one-time authorization code từ ứng dụng dành cho thiết bị di động. Tôi cần phải chuyển đổi nó thành mã thông báo truy cập mạng xã hội và mã thông báo làm mới và lưu chúng trên máy chủ DB để sử dụng sau này.Spring Social Google - chuyển đổi mã ủy quyền một lần thành mã thông báo truy cập/làm mới truy cập trên máy chủ

mã hiện tại của tôi:

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 là sai vì ConnectionData được mong đợi một access token và không phải là mã ủy quyền một thời gian. Bất kỳ ý tưởng làm thế nào để có được spring-social-google để trao đổi mã một lần cho mã thông báo truy cập và mã thông báo làm mới?

Trả lời

2

Đây là mã để trao đổi đang ủy quyền cho access token

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

Để làm cho nó đi đi của bạn cần phải yêu cầu offline access for Google. Chủ yếu là thay đổi chỉ là thêm tham số truy vấn 'access_type = offline' tuy nhiên bạn đã nhận được oneTimeAuthorizationCode đó. Sau đó, bạn sẽ nhận được mã thông báo làm mới lại sau khi ủy quyền.

Đối với dự án của riêng tôi, tôi đã kết thúc tùy ProviderSignInController để thêm param truy vấn bằng tay vì nó không cho phép bạn để vượt qua nó trong qua 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

Giải pháp:

 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(); 
Các vấn đề liên quan