无法刷新google_oauth2身份验证令牌错误。

时间:2018-08-05 12:41:30

标签: ruby-on-rails google-api google-oauth

我正在尝试从OAuth.io获取任何基于Google的提供商的访问令牌,但是,每当我进行身份验证时,我都会获得一个access_token但没有refresh_token。我为access_type选择了脱机,但仍然不满意。

    def google_auth
        # Create a new API client & load the Google Drive API
        client = Google::APIClient.new
        client.authorization.client_id = ENV['GOOGLE_ID']
        client.authorization.client_secret = ENV['GOOGLE_SECRET']
        client.authorization.scope = ENV['GOOGLE_SCOPE']
        client.authorization.redirect_uri = ENV['REDIRECT_URI']
        client.authorization.code = self.code.chomp || ""
        client.authorization.access_token = self.token
        client.authorization.refresh_token = self.refresh_token
      #  client.authorization.additional_parameters = {
      #  "access_type" => "offline",         # offline access
      #  "include_granted_scopes" => "true"  # incremental auth
      #


        if client.authorization.refresh_token &&
            client.authorization.expired?
            client.authorization.fetch_access_token!
         end
        return client
      end

      def refresh_google
        options = {
          body: {
            client_id: ENV['GOOGLE_ID'],
            client_secret: ENV['GOOGLE_SECRET'],
            refresh_token: self.refresh_token,
            grant_type: 'refresh_token',
            access_type: 'offline'
          },
          headers: {
            'Content-Type' => 'application/x-www-form-urlencoded',
          #  'access_type' =>'offline'
          }
        }
        @response = HTTParty.post('https://accounts.google.com/o/oauth2/token', options)
        if @response.code == 200
          self.token = @response.parsed_response['access_token']
          self.save
        else
          Rails.logger.error("Unable to refresh google_oauth2 authentication token.")
          Rails.logger.error("Refresh token response body: #{@response.body}")
        end
      end

请为此提供帮助

1 个答案:

答案 0 :(得分:0)

Issue resolved: I was trying to put 

"client.authorization.additional_parameters = {
        "access_type" => "offline",         # offline access
        "include_granted_scopes" => "true"  # incremental auth"
in User the rb model where callback was made but had to put it under session controller where API was called!