Google から Classroom のテスト アカウントを取得しました (つまり、Classroom 以外のすべてのサービスが無効になっています)。https://developers.google.com/classroom/quickstart/rubyの例に従って(わずかな変更を加えて)、ログインして、学生のコースなどの詳細を取得できます。
しかし、私がやりたいのは、Rails アプリで利用することです。omniauth-google-oauth2
and google-api-ruby-client
(新しい 0.9.x ブランチ)を使用しています。そのテスト アカウントのトークンを Google から取得した後 (使用している他のプロファイルではなく、このアカウントであることを 3 回確認しました)、 からデータにアクセスしようとしていますGoogle::Apis::ClassroomV1::ClassroomService
が、次のように表示されます。
HTTP を送信する https://classroom.googleapis.com/v1/courses?pageSize=10 403 #<Hurley::レスポンス GET https://classroom.googleapis.com/v1/courses?pageSize=10 == 403 (319 バイト) 748ms> キャッチされた禁止エラー: @NotGoogleAppsUser ユーザーは Google Apps ユーザーではありません。 エラー - #<Google::Apis::ClientError: 禁止: @NotGoogleAppsUser ユーザーは Google Apps ユーザーではありません。> Google::Apis::ClientError: 禁止: @NotGoogleAppsUser ユーザーは Google Apps ユーザーではありません。
私が使用するコードは次のとおりです。
「google/apis/classroom_v1」が必要です モジュール GoogleIntegration::クライアント 教室 = Google::Api::ClassroomV1 def self.create(トークン) サービス = 教室::ClassroomService.new scopes = ['https://www.googleapis.com/auth/classroom.courses.readonly', 'profile', 'email'] service.authorization = Google::Auth.get_application_default(スコープ) service.authorization.access_token = トークン service.client_options.application_name = 'メンブレン教室のサンプル' サービス 終わり
そしてコントローラーで:
service = GoogleIntegration::Client.create(request.env['omniauth.auth'].credentials.token) 応答 = service.list_courses(page_size: 10)