![](/img/trans.png)
[英]Google Sheets API v4 service_account - forbidden: The caller does not have permission (Google::Apis::ClientError)
[英]Google::Apis::ClientError: forbidden: The caller does not have permission when using service account call with ruby google api
我正在嘗試使用谷歌 api 的服務帳戶來處理課堂數據,目的是將我們的學校 web 服務與谷歌課堂數據同步。
我已將域范圍的權限委托給服務帳戶並激活了 Google 課堂 API。我已經下載了下面使用的 json 密鑰文件。 我已將https://www.googleapis.com/auth/classroom.courses添加到服務帳戶的 scope。
我在 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>"
確保您模擬的帳戶可以訪問此課程,否則您將遇到相同的錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.