如何授权google-api-ruby-client?

AnA*_*ice 7 ruby ruby-on-rails omniauth google-api-ruby-client

我正在努力让google-api-ruby-client gem按照基本用法示例进行操作:基本用法

require 'google/apis/drive_v2'

Drive = Google::Apis::DriveV2 # Alias the module
drive = Drive::DriveService.new
drive.authorization = ... # See Googleauth or Signet libraries

# Search for files in Drive (first page only)
files = drive.list_files(q: "title contains 'finances'")
files.items.each do |file|
  puts file.title
end
Run Code Online (Sandbox Code Playgroud)

我被困的地方是drive.authorization.我已经通过gem omniauth-google-oauth2为用户提供了一个授权令牌.如何在google-api-ruby-client中使用该令牌?

谢谢

Quv*_*Quv 5

我也被卡住了。IMO Google 应该在他们的文档中详细说明,特别是因为我们所要做的只是添加一个请求标头......

无论如何,这是一个例子。

#
# 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
Run Code Online (Sandbox Code Playgroud)
Drive = Google::Apis::DriveV2
drive = Drive::DriveService.new
drive.authorization = Google::AccessToken.new your_token
Run Code Online (Sandbox Code Playgroud)

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