Google:: api::ClientError: forbidden:调用者没有权限使用ruby Google ap



我正在尝试使用一个带有谷歌api的服务帐户来处理课堂数据,目标是将我们的学校网络服务与谷歌课堂数据同步。

我已将域范围的权限委托给服务帐户,并激活了Google Classroom 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 servicen #{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参数指定,如准备在库文档中进行授权的API调用一节所示:

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

注意:

确保您模拟的帐户可以访问本课程,否则您将得到相同的错误。

相关:

  • 将域权限委托给服务帐户
  • Google API Server-to-Server通信不工作(Ruby实现)

最新更新