これは、Classroom アドオンのウォークスルー シリーズの7 回目 のウォークスルーです。
このウォークスルーでは、ウェブ アプリケーションに動作を追加して、Google Classroom の外部からアドオンの添付ファイルを作成
します。この動作を使用すると、ユーザーは既存のプロダクトまたはウェブサイトからアドオンの添付ファイルを作成できます。また、CourseWork
との統合に最適です。フローを変更することなく、既存のトラフィックをアドオンが提供する改善されたユーザー
エクスペリエンスに誘導できるためです。推奨されるプロセスについては、添付ファイルを作成する
Classroom の外部でガイドページをご覧ください。
また、アドオンに動作を追加して、アドオンの添付ファイルを使用して課題をプログラムで変更 します。課題を作成したユーザーに関係なく、アドオンの添付ファイルが 1 つ以上ある課題を変更できます。これは、生徒がアクティビティを完了した後に課題を提出する場合に特に便利です。割り当てられたタスクが完了し、生徒の作業を確認できることを教師に通知できます。
コンテンツ タイプまたはアクティビティ タイプのアタッチメントをサポートするアドオンの最終バージョンを拡張します。このガイドでは、コンテンツ タイプのアタッチメントを使用します。
課題管理 OAuth スコープを追加する
アプリケーションが次のスコープをリクエストしていることを確認します。
https://www.googleapis.com/auth/classroom.addons.teacherhttps://www.googleapis.com/auth/classroom.addons.studenthttps://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 ハンドシェイクを完了する必要があります。具体的なガイダンスについては、ログイン のウォークスルーをご覧ください。
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 は、ユーザーがこれらの前提条件を満たしているかどうかを判断する
userProfiles.checkUserCapability メソッドを提供しています。前提条件を満たすユーザーは、対象 ユーザーと呼ばれます。
利用資格の確認を CourseWork 作成ルートの実装に追加します。
次に、レスポンスの allowed
フィールドをテストします。対象ユーザーの場合は、
ロジックに従ってアドオンの添付ファイルを含む課題を作成します。それ以外の場合は、リンク
マテリアルを作成します。ユーザーが課題を作成するコースの 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 ...
# Check whether the user can create add-on attachments.
eligibility_response = (
classroom_service.userProfiles()
.checkUserCapability(
userId="me",
capability="CREATE_ADD_ON_ATTACHMENT",
# The previewVersion is necessary while the method is available in the
# Workspace Developer Preview Program.
previewVersion="V1_20240930_PREVIEW",
).execute()
)
is_create_attachment_eligible = eligibility_response.get("allowed")
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
# The ID of the course to which the assignment will be added.
course_id = 1234567890 # TODO(developer) Replace with an actual course ID.
# /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
# The ID of the course to which the assignment will be added.
course_id = 1234567890 # TODO(developer) Replace with an actual course ID.
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()
)
アドオンをテストする
わかりやすくするため、提供されている例では、ハードコードされたコースと課題の識別子を使用しています。これらの識別子を取得するには、
教師の認証情報を使用して get と list メソッドを courses と
courseWork リソースにリクエストします。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 に戻って、課題のタイトルが変更されていることを確認します。
おめでとうございます。ウォークスルー シリーズを完了しました。