簡體   English   中英

如何在 Rails 上使用 Ruby 在本地測試谷歌 API?

[英]How can I test google APIs locally using Ruby on Rails?

我正在嘗試列出我的 Google Apps for Business 帳戶中的群組。 我設置了一個服務帳戶並賦予它服務目錄管理員角色。 我現在想在我的本地機器上測試它(我正在使用 RubyMine 通過 Rspec 運行它)。 測試如下所示:

require 'rails_helper'

describe GoogleApiHelper, type: :helper do
  it 'should authenticate successfully to Google' do
    google_auth_creds = OpenStruct.new authenticate(Api::Application.config.google[:auth][:scopes])
    expect(google_auth_creds).to_not be_nil
    expect(google_auth_creds.access_token).to match(/[A-Za-z\._-]*/)

    puts(google_auth_creds)
  end

  it 'should list all of the groups in the sinkingmoon.com domain' do
    groups = OpenStruct.new list_groups 'mydomain.com'

    puts(groups)
  end
end

被測試的實現如下所示:

require 'googleauth'
require 'google/apis/admin_directory_v1'

module GoogleApiHelper
  def authenticate(scopes)
    authorizer = Google::Auth::ServiceAccountCredentials.make_creds(
        json_key_io: File.open(Api::Application.config.google[:auth][:json_file]),
        scope: scopes)

    authorizer.fetch_access_token!
  end

  def retrieve_group(domain, groupId)
    authorizer = authenticate(Api::Application.config.google[:scopes])

    directory_service = Google::Apis::AdminDirectoryV1::DirectoryService.new
    directory_service.authorization = authorizer
    directory_service.get_group groupId
  end

  def list_groups(domain)
    authorizer = authenticate(Api::Application.config.google[:scopes])

    directory_service = Google::Apis::AdminDirectoryV1::DirectoryService.new
    directory_service.authorization = authorizer

    directory_service.list_groups(domain: domain)
  end
end

身份驗證測試按我的預期工作,因為它給了我一個授權令牌和一個過期時間。 但是,列出組的測試失敗,並顯示以下消息:

Google::Apis::AuthorizationError: Unauthorized

  0) GoogleApiHelper should retrieve a group by the domain
     Failure/Error: directory_service.get_group groupId

     Google::Apis::AuthorizationError:
       Unauthorized

我不太確定我做錯了什么。 我的憑據存儲在一個 .json 文件中,並且在環境/test.rb 文件中設置了配置:

  Api::Application.config.google = {
      :auth => {
          :json_file => 'config/auth/credentials.json',
          :scopes => [ 'https://www.googleapis.com/auth/admin.directory.group', 'https://www.googleapis.com/auth/admin.directory.domain' ]
      },
      :group => 'mygroup@mydomain.com'
  }

我想知道我是否實際上錯誤地設置了此服務帳戶的范圍。 或者,可能是我試圖從我的本地機器訪問 API,而不是從我注冊的域上的服務器? 如果是后者,我怎么能 go 在將其部署到我的生產或登台 webapp 之前對其進行測試?

更新我打印出應用程序正在使用的范圍,它們是: ["https://www.googleapis.com/auth/admin.directory.group", "https://www.googleapis.com/auth/admin.directory.domain"] 我相信這些是正確的。 我已將這些添加到管理控制台中的安全 -> 高級設置 -> 管理 API 客戶端訪問,並輸入我的服務帳戶的名稱,這恰好是類似test-account@myprojectname.iam.gserviceaccount.com的名稱,和上面列出的范圍。 這將轉換為服務帳戶的唯一 ID。

如果您真的從開發環境中得到響應,您是否嘗試過? 這是我用的,你可以試一試

require 'google/apis/analyticsreporting_v4'

AP = Google::Apis::AnalyticsreportingV4
credentials = Google::Auth::ServiceAccountCredentials.make_creds(json_key_io: IO.new(IO.sysopen('./config/credentials.json')))
credentials.scope = "https://www.googleapis.com/auth/analytics.readonly"
ga_reporting_service = AP::AnalyticsReportingService.new
ga_reporting_service.authorization = credentials.fetch_access_token!({})["access_token"]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM