How to authorize the google-api-ruby-client?

我们两清 提交于 2019-12-05 05:38:01

I too was stuck. IMO Google should have more elaborated in their documents, especially since all we had to do was just adding one request header...

Anyway, here is one example.

#
# Note I don't think we always have to define a class with such a conflict-prone name. 
# An anonymous class defined before every single API call should be also fine.
#
module Google
  class AccessToken
    attr_reader :token
    def initialize(token)
      @token = token
    end

    def apply!(headers)
      headers['Authorization'] = "Bearer #{@token}"
    end
  end
end
Drive = Google::Apis::DriveV2
drive = Drive::DriveService.new
drive.authorization = Google::AccessToken.new your_token

Reference: https://github.com/google/google-api-ruby-client/issues/296

Similarly to other answers I followed this approach, where the model storing the auth and refresh tokens is used, abstracting API interactions from that logic.

# Manages access tokens for users when using Google APIs
#
# Usage:
# require 'google/apis/gmail_v1'
# Gmail = Google::Apis::GmailV1 # Alias the module
# service = Gmail::GmailService.new
# service.authorization = GoogleOauth2Authorization.new user
# service.list_user_messages(user.email)
#
# See also:
# https://github.com/google/google-api-ruby-client/issues/296
class GoogleOauth2Authorization
  attr_reader :user

  def initialize(user)
    @user = user
  end

  def apply!(headers)
    headers['Authorization'] = "Bearer #{token}"
  end

  def token
    refresh! if user.provider_token_expires_at.past?
    user.provider_access_token
  end

  private

  def refresh!
    new_token = oauth_access_token(
      user.provider_access_token,
      user.provider_refresh_token
    ).refresh!
    if new_token.present?
      user.update(
        provider_access_token: new_token.token,
        provider_token_expires_at: Time.zone.at(new_token.expires_at),
        provider_refresh_token: new_token.refresh_token
      )
    end
    user
  end

  def oauth_access_token(access_token, refresh_token)
    OAuth2::AccessToken.new(
      oauth_strategy.client,
      access_token,
      refresh_token: refresh_token
    )
  end

  def oauth_strategy
    OmniAuth::Strategies::GoogleOauth2.new(
      nil,
      Rails.application.credentials.oauth[:google_id],
      Rails.application.credentials.oauth[:google_secret]
    )
  end
end
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!