これは、Classroom アドオン チュートリアル シリーズの 7 番目のチュートリアルです。
このチュートリアルでは、Google Classroom の外部からアドオンの添付ファイルを作成する動作をウェブ アプリケーションに追加します。この動作を使用すると、ユーザーが既存のサービスまたはウェブサイトからアドオンの添付ファイルを作成できるようになります。これは、フローを変更することなく、アドオンによって改善されたユーザー エクスペリエンスに既存のトラフィックを誘導するため、CourseWork
統合への優れた機能にもなります。推奨されるプロセスは、Classroom の外部で添付ファイルを作成するガイドページに記載されています。
また、アドオンにプログラムでアドオン アタッチメントを使用して割り当てを変更する動作を追加します。割り当ての作成者に関係なく、アドオンの添付ファイルがある課題を変更できます。これは、生徒がアクティビティを完了した後で課題を提出し、割り当てられたタスクが完了し、生徒の提出物をレビューする準備ができたことを教師に知らせる場合に特に便利です。
コンテンツ タイプまたはアクティビティ タイプのアタッチメントをサポートするアドオンの最終バージョンを拡張します。このガイドでは、コンテンツ タイプの添付ファイルを使用します。
割り当て管理の OAuth スコープを追加する
アプリケーションが次のスコープをリクエストすることを確認します。
https://www.googleapis.com/auth/classroom.addons.teacher
https://www.googleapis.com/auth/classroom.addons.student
https://www.googleapis.com/auth/classroom.coursework.students
これまで classroom.coursework.students
スコープは不要でした。このスコープは、CourseWork
割り当てを作成または変更するために使用されます。このスコープを、Cloud プロジェクトの Google Workspace Marketplace SDK、OAuth 同意画面、サーバーコードのスコープのリストに追加します。
Python
SCOPES = [
"https://www.googleapis.com/auth/classroom.addons.teacher",
"https://www.googleapis.com/auth/classroom.addons.student",
"https://www.googleapis.com/auth/classroom.coursework.students",
]
Classroom で課題を作成する
iframe を使用していないウェブページにボタンを追加する
このチュートリアルで説明するフローでは、Google 以外のプロダクトから Google Classroom の課題と添付ファイルを作成できます。実際には、既存のウェブサイトやアプリケーションが該当します。この例では、外部サイトとして機能する疑似ウェブページを作成する必要があります。クリックすると、推奨される CourseWork
フローを実行して新しい割り当てを作成する新しいルートを開くボタンまたはリンクが必要です。
また、ユーザーがログインできるボタンまたはリンクを追加する必要があります(まだ追加していない場合)。その後の API リクエストにはユーザー認証情報が必要になるため、ユーザーは OAuth 2.0 handshake を完了する必要があります。具体的なガイダンスについては、ログイン チュートリアルをご覧ください。
Python
提供されている Python の例では、最初のチュートリアル ステップで導入された /index
ルートを変更します。
<!-- /webapp/templates/index.html -->
<a href="clear-credentials.html">Logout</a>
<a href="start-auth-flow.html">Login</a>
<br>
<a href="create-coursework-assignment.html">Create a CourseWork Assignment</a>
ウェブサイトのリンク先を表す HTML テンプレートを追加します。このページは、CourseWork
課題に添付されるコンテンツを表します。
<!-- /webapp/templates/example-coursework-assignment.html -->
<h1>CourseWork assignment loaded!</h1>
<p>You've loaded a CourseWork assignment! It was created from an external web page.</p>
CourseWork 関連のルートを処理するための新しい Python モジュール ファイルを作成します。この例では coursework_routes.py
です。次の 3 つのルートを追加します。一部の内容は後で入力します。
# /webapp/coursework_routes.py
@app.route("/create-coursework-assignment")
def create_coursework_assignment():
"""
Completes the assignment creation flow.
"""
# Check that the user is signed in. If not, perform the OAuth 2.0
# authorization flow.
credentials = get_credentials()
if not credentials:
return start_auth_flow("coursework_assignment_callback")
# Construct the Google Classroom service.
classroom_service = get_classroom_service()
pass # To be completed later.
@app.route("/example-coursework-assignment/<assignment_type>")
def example_coursework_assignment(assignment_type):
"""
Renders the "example-coursework-assignment.html" template.
"""
return flask.render_template(
"example-coursework-assignment.html", assignment_type=assignment_type
)
@app.route("/coursework-assignment-callback")
def coursework_assignment_callback():
"""
Completes the OAuth 2.0 handshake and stores credentials in the session.
This is identical to the callback introduced in the sign-in walkthrough,
but redirects the user to the index page instead of the attachment
discovery page.
"""
flow = google_auth_oauthlib.flow.Flow.from_client_secrets_file(
CLIENT_SECRETS_FILE,
scopes=SCOPES,
state=flask.session["state"],
redirect_uri=flask.url_for("coursework_assignment_callback", _external=True),
)
flow.fetch_token(authorization_response=flask.request.url)
credentials = flow.credentials
flask.session["credentials"] = session_credentials_to_dict(
credentials
)
# Close the current window and redirect the user to the index page.
return flask.render_template("close-me.html", redirect_destination="index")
ユーザーのアドオン作成の適格性を確認する
ユーザーの代わりにアドオンのアタッチメントを作成するには、ユーザーが満たさなければならない前提条件がいくつかあります。便宜上、Google ではユーザーがこれらの前提条件を満たしているかどうかを判断するための courses.checkAddOnCreationEligibility
メソッドを提供しています。要件を満たすユーザーは、適格ユーザーと呼ばれます。
CourseWork
作成ルートの実装に、適格性チェックを追加します。次に、レスポンスの isCreateAttachmentEligible
フィールドをテストします。対象となるユーザーの場合は、ロジックに沿ってアドオン アタッチメント付きの課題を作成します。それ以外の場合は、リンク マテリアルを作成します。ユーザーが課題を作成するコースの ID が必要です。通常は 使用するコースを
ユーザーに指定するよう促しますわかりやすくするために、この例ではハードコードされた値を使用しています。
Python
# /webapp/coursework_routes.py
@app.route("/create-coursework-assignment")
def create_coursework_assignment():
"""
Completes the assignment creation flow.
"""
# ... Check that the user is signed in and get the Classroom service ...
# The ID of the course to which the assignment will be added.
course_id = 1234567890 # TODO(developer) Replace with an actual course ID.
# Check whether the user can create add-on attachments.
eligibility_response = (
classroom_service.courses()
.checkAddOnCreationEligibility(courseId=course_id)
.execute()
)
is_create_attachment_eligible = eligibility_response.get("isCreateAttachmentEligible")
if is_create_attachment_eligible:
# See the "Create an assignment with add-on attachment for eligible users" section for implementation.
if not is_create_attachment_eligible:
# See the "Create a Link Material" section for implementation.
アドオンの添付ファイルを含む課題を対象ユーザー向けに作成する
ユーザーにアドオンの添付ファイルを作成する権限がある場合は、次の操作を行います。
- API リクエストを送信して、Google Classroom で添付ファイルなしで課題
courseWork
を作成します。 - 新しく作成した課題の
id
を抽出します。 - 新しい CourseWork
AddOnAttachment
を作成します。 - Google Classroom で新しく作成された課題にアドオンの添付ファイルを作成するリクエストを送信します。
Python
# /webapp/coursework_routes.py
if is_create_attachment_eligible:
# Create an assignment.
coursework = {
"title": "My CourseWork Assignment with Add-on Attachment",
"description": "Created using the Classroom CourseWork API.",
"workType": "ASSIGNMENT",
"state": "DRAFT", # Set to 'PUBLISHED' to assign to students.
}
# Issue a request to create the assignment.
create_assignment_response = (
classroom_service.courses()
.courseWork()
.create(courseId=course_id, body=coursework)
.execute()
)
# Create an add-on attachment that links to the selected content and
# associate it with the new assignment.
content_url = flask.url_for(
"example_coursework_assignment",
assignment_type="add-on-attachment",
_scheme="https",
_external=True,
)
# Construct an AddOnAttachment instance.
attachment = {
"teacherViewUri": {"uri": content_url},
"studentViewUri": {"uri": content_url},
"title": f'Test Attachment for Assignment {create_assignment_response.get("id")}',
}
# Issue a request to create the attachment.
add_on_attachment_response = (
classroom_service.courses()
.courseWork()
.addOnAttachments()
.create(
courseId=course_id,
itemId=create_assignment_response.get("id"), # ID of the new assignment.
body=attachment,
)
.execute()
)
リンクのマテリアルを作成する
ユーザーにアドオンの添付ファイルを作成する資格がない場合は、代わりに次の手順でリンク マテリアルを作成します。
Python
if not is_create_attachment_eligible:
coursework = {
"title": "My CourseWork Assignment with Link Material",
"description": "Created using the Classroom CourseWork API.",
"workType": "ASSIGNMENT",
"state": "DRAFT", # Set to 'PUBLISHED' to assign to students.
# Specify the URL for your content as a Link Material.
"materials": [
{
"link": {
"url": flask.url_for(
"example_coursework_assignment",
assignment_type="link-material",
_scheme="https",
_external=True,
)
}
}
],
}
# Issue a request to create the assignment.
assignment_response = (
classroom_service.courses()
.courseWork()
.create(courseId=course_id, body=coursework)
.execute()
)
作成済みの割り当てを変更する
ストリーム アイテムの作成者に関係なく、アドオンが 1 つ以上添付されている Google Classroom ストリーム アイテムへのアクセス、変更、提出、再利用、返却を行うことができます。ストリーム アイテムは、Announcement
、CourseWork
の割り当て、または CourseWorkMaterial
です。
それを確認するために、特定のストリーム アイテムを変更するルートを追加します。このメソッドを使用して、自身が API を使用して作成したストリーム アイテムと、Google Classroom UI を使用して教師が作成したストリーム アイテムにアクセスして変更できることを確認します。
このチュートリアルで最初に編集したウェブページにリンクまたはボタンを 1 つ以上追加します。新しいルートが開き、CourseWork
割り当てが変更されます。
Python
提供されている Python の例では、このチュートリアルの前半に変更された /index
ルートを変更します。
<!-- /webapp/templates/index.html -->
<a href="modify-coursework-assignment.html">Create a CourseWork Assignment</a>
CourseWork 関連のルートを処理する新しいルートを作成します。上の例では coursework_routes.py
ファイル内にあります。
# Check that the user is signed in.
credentials = get_credentials()
if not credentials:
return start_auth_flow("coursework_assignment_callback")
# Get the Google Classroom service.
classroom_service = get_classroom_service()
# The ID of the course to which the assignment will be added.
# Ordinarily, you'll prompt the user to specify which course to use. For
# simplicity, we use a hard-coded value in this example.
course_id = 1234567890 # TODO(developer) Replace with an actual course ID.
assignment_id = 1234567890 # TODO(developer) Replace with an actual assignment ID.
# Retrieve details about the CourseWork assignment.
get_coursework_response = (
classroom_service.courses()
.courseWork()
.get(courseId=course_id, id=assignment_id)
.execute()
)
# Alter the current title.
assignment_title = f"{get_coursework_response.get('title')} (Modified by API request)"
# Issue a request to modify the assignment.
modify_coursework_response = (
classroom_service.courses()
.courseWork()
.patch(
courseId=course_id,
id=assignment_id,
updateMask="title",
body={"title": assignment_title},
)
.execute()
)
アドオンをテストする
説明をわかりやすくするため、ここで提供されている例では、ハードコードされたコースと課題の識別子を使用しています。これらの ID を取得するには、教師の認証情報を使用して、courses
リソースと courseWork
リソースの get
メソッドと list
メソッドにリクエストを送信します。また、courseWork
割り当ての作成時にもレスポンスで返されます。
リンク マテリアルの作成をテストする
サーバーを実行し、インデックス ページに移動して、Google Workspace for Education Teaching & Learning または Plus のライセンスを持っていない教師ユーザーとしてログインします。テストドメインの管理コンソールでユーザーのライセンス ステータスを切り替えることができます。[Create a CourseWork Assignment] ボタンをクリックしてから、Google Classroom の UI を開き、リンク教材の添付ファイルを含む課題が作成されたことを確認します。添付ファイルには、リンクされたウェブページのタイトルと URL が表示されます。
アドオンの添付ファイルの作成をテストする
インデックス ページに戻り、Google Workspace for Education Teaching & Learning または Plus のライセンスを持つ教師ユーザーとしてログインします。[Create a CourseWork Assignment] ボタンをクリックして Google Classroom UI を開き、アドオン添付ファイルを含む課題が作成されたことを確認します。添付ファイルには、コードで指定したアドオン アプリケーションの名前とタイトルが表示されます。
課題の変更をテストする
インデックス ページに戻り、Teaching & Learning または Plus のライセンスを持つ教師ユーザーとしてログインしていることを確認します。[Modify a CourseWork Assignment] ボタンをクリックし、Google Classroom の UI に戻り、課題のタイトルが変更されていることを確認します。
これで完了です。これでチュートリアル シリーズは終了です。