Google::Apis::ClientError: forbidden: The caller does not have permission when using service account call with ruby google api

Google::Apis::ClientError: forbidden: The caller does not have permission when using service account call with ruby google api

我正在尝试使用 google 的 api 的服务帐户来处理课堂数据,目的是将我们的学校 Web 服务与 google 课堂数据同步.

我已将全域权限委托给服务帐户并激活了 Google 课堂 API。我已经下载了下面使用的 json 密钥文件。 我已将 https://www.googleapis.com/auth/classroom.courses 添加到服务帐户的范围。

我的测试代码在app/models/g_service.rb:

class GService  
  require 'google/apis/classroom_v1'

  def get_course
    authorizer = Google::Auth::ServiceAccountCredentials.make_creds(
      json_key_io: File.open('/Users/jose/Downloads/skt1-301603-4a655caa8963.json'),
      scope: [ Google::Apis::ClassroomV1::AUTH_CLASSROOM_COURSES ]
    )
    authorizer.fetch_access_token!
    service = Google::Apis::ClassroomV1::ClassroomService.new
    service.authorization = authorizer

    puts "\n service\n #{service.inspect}"
    response = service.get_course( '99999' )
    puts "\n response \n#{response.inspect}"
  end
end

控制台中的结果是:

>> GService.new.get_course

 service
 #<Google::Apis::ClassroomV1::ClassroomService:0x007fe1cff98338 @root_url="https://classroom.googleapis.com/", @base_path="", @upload_path="upload/", @batch_path="batch", @client_options=#<struct Google::Apis::ClientOptions application_name="unknown", application_version="0.0.0", proxy_url=nil, open_timeout_sec=nil, read_timeout_sec=nil, send_timeout_sec=nil, log_http_requests=false, transparent_gzip_decompression=true>, @request_options=#<struct Google::Apis::RequestOptions authorization=#<Google::Auth::ServiceAccountCredentials:0x0xxxxxxx @project_id="sssssssss", @authorization_uri=nil, @token_credential_uri=#<Addressable::URI:0x000000000 URI:https://www.googleapis.com/oauth2/v4/token>, @client_id=nil, @client_secret=nil, @code=nil, @expires_at=2021-01-13 20:56:46 -0800, @issued_at=2021-01-13 19:56:47 -0800, @issuer="xxxxxxx.iam.gserviceaccount.com", @password=nil, @principal=nil, @redirect_uri=nil, @scope=["https://www.googleapis.com/auth/classroom.courses"], @state=nil, @username=nil, @access_type=:offline, @expiry=60, @audience="https://www.googleapis.com/oauth2/v4/token", @signing_key=#<OpenSSL::PKey::RSA:0xxxxxxxxx>, @extension_parameters={}, @additional_parameters={}, @connection_info=nil, @grant_type=nil, @refresh_token=nil, @access_token="-------------------------------------------------------------------------------------->, retries=0, header=nil, normalize_unicode=false, skip_serialization=false, skip_deserialization=false, api_format_version=nil, use_opencensus=true>>
Google::Apis::ClientError: forbidden: The caller does not have permission

在 service.get_course('99999') 调用之前似乎一切正常。 我已经使用 https://developers.google.com/classroom/reference/rest/v1/courses/get 在线工具测试了这个调用,它工作正常。

我翻阅了文档,但无法解决这个问题。 任何人都可以让我知道我错过了什么吗?

我是 运行 rails 3.2 和 ruby 2.1

考虑到您遇到的错误,我认为您没有冒充任何帐户。

全域委派的目的是服务帐户可以模拟您域中的常规帐户,但为此,您必须指定要模拟的帐户。否则,您将自己调用服务帐户,您是否已为其启用全域委派并不重要。

在 Ruby 库中,您可以使用 :sub 参数指定,如库文档的 Preparing to make an authorized API call 部分所示:

authorizer.sub = "<email-address-to-impersonate>"

注:

确保您模拟的帐户可以访问此课程,否则您将遇到同样的错误。

相关: