我有一個來自Google的Classroom測試帳戶(即,除Classroom以外的所有服務都已禁用)。按照https://developers.google.com/classroom/quickstart/ruby(稍作修改)中的示例,我可以登錄並獲取詳細信息,例如關於學生的課程。Google課堂,Ruby,Rails:@NotGoogleAppsUser with test account
但是我想要做的就是在Rails應用中使用它。我正在使用omniauth-google-oauth2
和google-api-ruby-client
(新的0.9.x分支)。從谷歌提取該測試帳戶的令牌後(我已經三重檢查,它是此帳戶,而不是我使用的任何其他配置文件)我試圖從Google::Apis::ClassroomV1::ClassroomService
訪問數據,但它始終告訴我:
Sending HTTP get https://classroom.googleapis.com/v1/courses?pageSize=10 403 #<Hurley::Response GET https://classroom.googleapis.com/v1/courses?pageSize=10 == 403 (319 bytes) 748ms> Caught error forbidden: @NotGoogleAppsUser The user is not a Google Apps user. Error - #<Google::Apis::ClientError: forbidden: @NotGoogleAppsUser The user is not a Google Apps user.> Google::Apis::ClientError: forbidden: @NotGoogleAppsUser The user is not a Google Apps user.
我使用的代碼是:
require 'google/apis/classroom_v1' module GoogleIntegration::Client Classroom = Google::Apis::ClassroomV1 def self.create(token) service = Classroom::ClassroomService.new scopes = ['https://www.googleapis.com/auth/classroom.courses.readonly', 'profile', 'email'] service.authorization = Google::Auth.get_application_default(scopes) service.authorization.access_token = token service.client_options.application_name = 'Membean Classroom Sample' service end
和在控制器:
service = GoogleIntegration::Client.create(request.env['omniauth.auth'].credentials.token) response = service.list_courses(page_size: 10)
不,正如我所說的,用戶是來自Google的測試帳戶(在classroom-dev.com域中),這是一個有效的課堂啓用帳戶,因爲我使用https代碼訪問其詳細信息://developers.google.com/classroom/quickstart/ruby。只是Rails的寶石似乎無法使用它。 –