ruby-on-rails - Google::Apis::ClientError: 禁止:使用 ruby google api 使用服务帐户调用时调用者没有权限
问题描述
我正在尝试使用带有 google api 的服务帐户来处理课堂数据,目的是将我们的学校网络服务与 google 课堂数据同步。
我已将域范围的权限委托给服务帐户并激活了 Google Classroom API。我已经下载了下面使用的 json Key 文件。我已将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
参数,如库文档中的准备进行授权 API 调用部分所示:
authorizer.sub = "<email-address-to-impersonate>"
笔记:
确保您模拟的帐户有权访问此课程,否则您将收到相同的错误。
有关的:
推荐阅读
- sql - 更新字段时,会为所有记录触发日期修改触发器,而不仅仅是正在修改的记录
- c - 如何在 OpenSSL 1.1.1 中预先准备 DTLS 服务器
- python - `train=True` 在 H2O model_performance() 中的作用是什么?
- c# - 从 .mp3 文件中获取“初始密钥”值
- c++ - 使用 bicubic c++ 的 resize 函数的 opencv 代码流是什么?
- php - GoDaddy 的 PHP cURL SSL 问题:SSL 证书问题:无法获取本地颁发者证书
- c - 需要一个示例来检索 Gstreamer C 代码中输出流的流统计信息吗?
- java - 在 IntelliJ 中运行 OptaPlanner 示例
- regex - 为什么 zsh 没有正确执行我的正则表达式?
- google-apps-script - 使用数据透视表中的脚本更新不同的选项卡???