這是 Classroom 外掛程式系列的 seventh 逐步操作說明。
在本逐步操作說明中,您會新增行為至網頁應用程式,從 Google Classroom 以外的位置建立外掛程式附件。請使用這個行為,讓使用者從現有產品或網站建立外掛程式附件。這也適用於 CourseWork
整合,因為您可以將現有流量導向至外掛程式提供的更優質的使用者體驗,而不必變更流程。如需建議程序,請參閱「在 Classroom 以外建立附件」指南頁面。
此外,您也可以在外掛程式中新增行為,透過程式輔助方式修改具有外掛程式附件的指派作業。無論作業的建立者為何,您可以修改任何含有外掛程式附件的作業。這種做法特別適合在學生完成活動後繳交作業,通知老師指派的工作已經完成,學生作業也已可檢閱。
擴充最終版本且支援 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 要求,在 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()
)
建立連結素材
如果使用者「不符合」建立外掛程式附件的資格,請改為按照下列步驟建立連結 Material 元件:
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
。
為了示範,您需要新增路徑來修改指定串流項目。使用這個方法時,請確認您「以及」老師透過 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。如要取得這些 ID,您可以使用老師憑證來要求 courses
和 courseWork
資源的 get
和 list
方法。建立 courseWork
指派作業時,也會在回應中傳回這些字串。
測試連結材質建立
執行伺服器,然後前往索引頁面,以老師使用者身分登入,並在沒有 Google Workspace for Education Teaching & Learning 或 Plus 授權的情況下登入。您可以在測試網域的管理控制台中切換使用者的授權狀態。按一下「Create a CourseWork Assignment」按鈕,開啟 Google Classroom UI,並確認含有連結 Material 附件的作業已建立完成。附件應會顯示連結網頁的標題和網址。
測試外掛程式附件的建立作業
返回索引頁面,並以老師使用者的身分登入 Google Workspace for Education Teaching and Learning 或 Plus 授權。按一下「Create a CourseWork Assignment」按鈕,開啟 Google Classroom UI,並確認含有外掛程式附件的作業已建立完成。附件應會顯示外掛程式應用程式名稱,以及程式碼中指定的標題。
測試指派的修改作業
返回索引頁面,確認您已以老師使用者身分登入,並擁有 Teaching & Learning 或 Plus 授權。按一下「Modify a CourseWork Assignment」按鈕,然後返回 Google Classroom UI 確認作業標題已變更。
恭喜!您已完成本逐步教學系列課程。