How do I refresh my google_oauth2 access token using my refresh token?

后端 未结 2 1356
误落风尘
误落风尘 2020-12-18 00:19

I have a RoR app where I am authenticating against Google using omniauth and google_oauth2 where I am requesting offline access.

How do I use my refresh token to req

2条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-18 00:41

    For an example using the Ruby HTTParty gem:

    Where @auth is an ActiveRecord record that stores the auth keys for the specific user you are trying to refresh tokens for.

      # Refresh auth token from google_oauth2 and then requeue the job.
      options = {
        body: {
          client_id: ,
          client_secret: ,
          refresh_token: @auth.refresh_token,
          grant_type: 'refresh_token'
        },
        headers: {
          'Content-Type' => 'application/x-www-form-urlencoded'
        }
      }
      @response = HTTParty.post('https://accounts.google.com/o/oauth2/token', options)
      if @response.code == 200
        @auth.token = @response.parsed_response['access_token']
        @auth.expires_in = DateTime.now + @response.parsed_response['expires_in'].seconds
        @auth.save        
      else
        Rails.logger.error("Unable to refresh google_oauth2 authentication token.")
        Rails.logger.error("Refresh token response body: #{@response.body}")
      end
    

提交回复
热议问题