Google Apps Script クイックスタート

クイックスタートでは、Google Workspace API を呼び出すアプリを設定して実行する方法について説明します。

Google Workspace クイックスタートでは、API クライアント ライブラリを使用して認証と認可のフローの詳細を処理します。独自のアプリでは、クライアント ライブラリを使用することをおすすめします。このクイックスタートでは、テスト環境に適した簡素化された認証方法を使用します。本番環境では、アプリに適したアクセス認証情報を選択する前に、認証と認可について確認することをおすすめします。

Google Classroom API にリクエストを送信する Google Apps Script を作成します。

目標

  • スクリプトを作成します。
  • Google Classroom API を有効にします。
  • サンプルを実行します。

Prerequisites

  • Google Classroom が有効になっている Google for Education アカウント。

  • Google ドライブへのアクセス

スクリプトを作成する

  1. script.google.com/create にアクセスして、新しいスクリプトを作成します。
  2. スクリプト エディタの内容を次のコードで置き換えます。

classroom/quickstart/quickstart.gs
/**
 * Lists 10 course names and ids.
 */
function listCourses() {
  /**  here pass pageSize Query parameter as argument to get maximum number of result
   * @see https://developers.google.com/classroom/reference/rest/v1/courses/list
   */
  const optionalArgs = {
    pageSize: 10
    // Use other parameter here if needed
  };
  try {
    // call courses.list() method to list the courses in classroom
    const response = Classroom.Courses.list(optionalArgs);
    const courses = response.courses;
    if (!courses || courses.length === 0) {
      console.log('No courses found.');
      return;
    }
    // Print the course names and IDs of the courses
    for (const course of courses) {
      console.log('%s (%s)', course.name, course.id);
    }
  } catch (err) {
    // TODO (developer)- Handle Courses.list() exception from Classroom API
    // get errors like PERMISSION_DENIED/INVALID_ARGUMENT/NOT_FOUND
    console.log('Failed with error %s', err.message);
  }
}

  1. [保存] をクリックします。
  2. [無題のプロジェクト] をクリックし、「Quickstart」と入力して [名前を変更] をクリックします。

Google Classroom API を有効にする

  1. Apps Script プロジェクトを開きます。
  2. [編集者] をクリックします。
  3. [サービス] の横にある [サービスを追加] をクリックします。
  4. [Google Classroom API] を選択し、[追加] をクリックします。

サンプルの実行

Apps Script エディタで [実行] をクリックします。

サンプルを初めて実行すると、アクセスの承認を求めるプロンプトが表示されます。

  1. [権限を確認] をクリックします。
  2. アカウントを選択してください。
  3. [許可] をクリックします。

スクリプトの実行ログがウィンドウの下部に表示されます。

次のステップ