這是 Classroom 外掛程式的 seventh 逐步操作說明 這一系列的影片
在這個逐步操作說明中,您要在網頁應用程式中新增行為,以建立外掛程式
附加在 Google Classroom 外的附件使用此行為
使用者可從您現有的產品或網站建立外掛程式附件。這是
CourseWork
整合的優點,在於
將流量導向外掛程式提供的優質使用者體驗
導入負責任的 AI 技術如要查看建議程序,請參閱「建立附件
指南頁面。
您也可以在外掛程式中新增行為,使用外掛程式修改作業 附加檔案您可以修改任何具有上述任一標記的作業。 您的外掛程式附件,無論作業建立者為何。這是 這項功能特別適合在學生等學生畢業後繳交作業 通知老師指派的工作已完成, 學生作業已可送交審閱。
您要擴充最終版本 (支援 content-type) 或 活動類型附件。本指南使用的是內容類型附件。
新增指派管理 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 握手。請參閱登入 逐步操作說明。
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>
建立新的 Python 模組檔案以處理 CourseWork 相關路徑。
這是我們提供的範例 coursework_routes.py
。新增下列項目
則只有三個路徑請注意,您稍後會填入其中的內容
# /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 要求,以便在以下位置建立
courseWork
作業: 含有無附件的 Google Classroom。 - 擷取新建立作業的
id
。 - 建立新的 CourseWork
AddOnAttachment
。 - 傳送要求,以便在新建的外掛程式上建立外掛程式附件 作業。
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()
)
建立連結素材
如果使用者不符合建立外掛程式附件的資格,請建立連結 如要使用 Material Design,請採取下列做法:
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()
)
修改已建立的作業
您可以存取、修改、繳交、收回或發還任何 Google Classroom
串流項目中至少含有一個外掛程式附件 (無論使用者是誰)
已建立訊息串項目。訊息串項目可以是任何Announcement
、CourseWork
作業或 CourseWorkMaterial
。
為了示範,您需要新增路徑來修改指定串流項目。使用這份草稿 方法驗證,您是否可以存取及修改自己建立的訊息串項目 使用 API,「以及」由老師透過 Google Classroom UI 建立。
為首次編輯的網頁加入一或多個連結或按鈕
這部影片將逐步介紹瀏覽器應該會開啟新路徑以修改 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
的 get
和 list
方法,
courseWork
資源。也會在建立容器時
courseWork
份作業。
測試連結材質建立
執行伺服器,然後前往索引頁面,以老師使用者身分登入 沒有 Google Workspace for Education 教學和Learning 或 Plus 授權。您可以切換 在測試網域的測試網域中 管理控制台。按一下「Create a CourseWork Assignment」(建立課程作業作業) 按鈕,開啟 Google Classroom UI,並使用 已建立連結教材附件。附件中會顯示 連結的網頁和網址
測試外掛程式附件的建立作業
返回索引頁面,並以老師使用者身分登入 Google Workspace for Education 教學。 &Learning 或 Plus 授權。按一下 [Create a CourseWork Assignment] (建立課程作業作業) 按鈕,然後開啟 Google Classroom UI 已建立外掛程式附件。附件中應會顯示 外掛程式應用程式,以及程式碼中指定的標題
測試指派的修改作業
返回索引頁面並確實以老師使用者身分登入 提供教學Learning 或 Plus 授權。按一下「修改 CourseWork」 [作業]按鈕,然後返回 Google Classroom UI 確認 作業標題已變更。
恭喜!您已完成本逐步教學系列課程。