【问题标题】:Spring Social Google - converting a one-time authorization code into an access token/ refresh token on the serverSpring Social Google - 将一次性授权代码转换为服务器上的访问令牌/刷新令牌
【发布时间】:2014-08-29 09:15:34
【问题描述】:

服务器从移动应用接收one-time authorization code。我需要将其转换为 spring-social 访问令牌并刷新令牌并将它们保存在服务器数据库中以供以后使用。

我当前的代码:

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

【问题讨论】:

    标签: java spring spring-social google-signin spring-social-google


    【解决方案1】:

    这是用授权码交换访问令牌的代码

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

    【讨论】:

      【解决方案2】:

      要让它进行,您需要请求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());
          }
      }
      

      【讨论】:

        【解决方案3】:

        解决办法:

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

        【讨论】:

          猜你喜欢
          • 2018-02-03
          • 2015-11-28
          • 2015-02-24
          • 2017-02-07
          • 2019-03-16
          • 2014-10-12
          • 1970-01-01
          • 2020-12-07
          • 2021-07-01
          相关资源
          最近更新 更多