นี่คือคำแนะนำแบบทีละขั้นที่ 7 ในชุดคำแนะนำแบบทีละขั้นเกี่ยวกับส่วนเสริมของ Classroom
ในคำแนะนำแบบทีละขั้นนี้ คุณจะเพิ่มลักษณะการทำงานในเว็บแอปพลิเคชันเพื่อสร้างไฟล์แนบของส่วนเสริมจากภายนอก 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
รายการ เพิ่มขอบเขตนี้ลงในรายการขอบเขตใน 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 Classroom และไฟล์แนบจากผลิตภัณฑ์ที่ไม่ใช่ของ Google ได้ โดยในทางปฏิบัติ นี่อาจเป็นเว็บไซต์หรือแอปพลิเคชันที่คุณมีอยู่ สำหรับตัวอย่างนี้ คุณต้องสร้างหน้าเว็บจำลองเพื่อให้ทำหน้าที่เป็นเว็บไซต์ภายนอก คุณต้องมีปุ่มหรือลิงก์ที่คลิกเพื่อเปิดเส้นทางใหม่ที่ดำเนินการขั้นตอน 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
ในตัวอย่างที่เราให้ไว้ เพิ่มเส้นทาง 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
ในคำตอบ สำหรับผู้ใช้ที่มีสิทธิ์ ให้ทำตามตรรกะเพื่อสร้างงานที่มีไฟล์แนบของส่วนเสริม หรือสร้างเนื้อหาลิงก์ คุณจำเป็นต้องทราบรหัสหลักสูตรที่ผู้ใช้ต้องการสร้างงาน โดยปกติแล้ว คุณจะได้รับแจ้ง
ให้ผู้ใช้ระบุว่าจะใช้หลักสูตรใด เพื่อความง่าย เราจะใช้ค่าฮาร์ดโค้ดในตัวอย่างนี้
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
ใหม่ - ส่งคำขอสร้างไฟล์แนบของส่วนเสริมในงานที่สร้างขึ้นใหม่ใน 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()
)
สร้างเนื้อหาลิงก์
หากผู้ใช้ไม่มีสิทธิ์สร้างไฟล์แนบของส่วนเสริม ให้สร้าง Link 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 ที่มีไฟล์แนบของส่วนเสริมอย่างน้อย 1 รายการ ไม่ว่าใครจะเป็นผู้สร้างรายการสตรีมก็ตาม รายการสตรีมเป็นงาน Announcement
, CourseWork
หรือ CourseWorkMaterial
ในการสาธิตนี้ คุณจะต้องเพิ่มเส้นทางเพื่อแก้ไขรายการสตรีมที่ระบุ ใช้วิธีการนี้เพื่อยืนยันว่าคุณสามารถเข้าถึงและแก้ไขรายการสตรีมที่คุณสร้างโดยใช้ API และที่ครูสร้างผ่าน UI ของ Google Classroom
เพิ่มลิงก์หรือปุ่มอีก 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 and Learning หรือ Plus คุณจะสลับสถานะใบอนุญาตของผู้ใช้ได้จากคอนโซลผู้ดูแลระบบของโดเมนทดสอบ คลิกปุ่มสร้างงานในหลักสูตร จากนั้นเปิด UI ของ Google Classroom และยืนยันว่ามีการสร้างงานที่มีไฟล์แนบใน Link Material แล้ว ในไฟล์แนบควรแสดงชื่อ ของหน้าเว็บและ URL ที่ลิงก์
ทดสอบการสร้างไฟล์แนบของส่วนเสริม
กลับไปที่หน้าดัชนีแล้วลงชื่อเข้าใช้ในฐานะผู้ใช้ที่เป็นครูที่มีใบอนุญาต Google Workspace for Education Teaching and Learning หรือ Plus คลิกปุ่มสร้างงานในหลักสูตร แล้วเปิด UI ของ Google Classroom และยืนยันว่างานที่มีไฟล์แนบของส่วนเสริมได้รับการสร้างขึ้นแล้ว ไฟล์แนบควรแสดงชื่อแอปพลิเคชัน ส่วนเสริมและชื่อที่ระบุไว้ในโค้ด
ทดสอบการแก้ไขการมอบหมาย
กลับไปที่หน้าดัชนีและตรวจสอบว่าได้ลงชื่อเข้าใช้ในฐานะผู้ใช้ของครูที่มีใบอนุญาต Teaching and Learning หรือ Plus คลิกปุ่มแก้ไขงานในหลักสูตร แล้วกลับไปที่ UI ของ Google Classroom และยืนยันว่ามีการแก้ไขชื่องาน
ยินดีด้วย คุณดำเนินชุดคำแนะนำแบบทีละขั้นจบแล้ว