如何授权google-api-ruby-client? [英] How to authorize the google-api-ruby-client?
问题描述
我正在按照下面的基本用法示例来工作google-api-ruby-client gem:基本用法
I'm working to get the google-api-ruby-client gem working following the basic usage example here: Basic usage
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
我被卡住的地方是drive.authorization
.我已经通过gem omniauth-google-oauth2为用户提供了授权令牌.如何在google-api-ruby-client中使用该令牌?
Where I'm stuck is drive.authorization
. I have an authorized token already for the user via the gem omniauth-google-oauth2. How do I use that token with google-api-ruby-client?
推荐答案
我也被困住了. IMO Google应该在其文档中进行更详细的说明,尤其是因为我们要做的只是添加一个请求标头...
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...
反正这是一个例子.
#
# 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
参考: https://github.com/google/google -api-ruby-client/issues/296
这篇关于如何授权google-api-ruby-client?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!