แอปแชทที่ใช้การตรวจสอบสิทธิ์ผู้ใช้ต้องรองรับสิทธิ์ OAuth แบบละเอียด เพื่อให้ผู้ใช้ให้สิทธิ์ขอบเขตย่อยของขอบเขตที่ขอได้ เช่น ผู้ใช้อาจ ให้สิทธิ์เข้าถึงชื่อของตนเอง แต่ปฏิเสธการเข้าถึงปฏิทิน
การจัดการสิทธิ์ OAuth แบบละเอียดจะขึ้นอยู่กับวิธีที่คุณสร้างแอป Chat ดังนี้
- ส่วนเสริม Apps Script ของ Google Workspace ที่ขยายการทำงานของ Chat
- แอป Chat ของ Apps Script แบบสแตนด์อโลน
- ส่วนเสริม Google Workspace แบบ HTTP ที่ขยาย Chat
- แอป Chat HTTP แบบสแตนด์อโลน
Apps Script
หากคุณสร้างแอปใน Chat โดยใช้ Apps Script Apps Script จะจัดการสิทธิ์ OAuth แบบละเอียดโดยอัตโนมัติ อย่างไรก็ตาม โปรดตรวจสอบว่าโค้ดของคุณจัดการกรณีที่ผู้ใช้ไม่ให้สิทธิ์เข้าถึงขอบเขตที่ขอทั้งหมดได้ วิธีการนี้ขึ้นอยู่กับว่า Apps Script ของคุณเป็นส่วนเสริมของ Google Workspace ที่ขยาย Google Chat โดยใช้ Apps Script หรือเป็นแอป Chat แบบสแตนด์อโลนที่สร้างด้วย Apps Script และเหตุการณ์การโต้ตอบ
ส่วนเสริมของ Google Workspace ที่ขยายการทำงานของ Chat
หากคุณสร้างแอป Chat เป็น ส่วนเสริม Google Workspace ที่ขยาย Google Chat โดยใช้ Apps Script ให้ทำตามวิธีการใน จัดการสิทธิ์ OAuth แบบละเอียดใน Apps Script
แอป Chat ของ Apps Script แบบสแตนด์อโลน
หากคุณสร้างแอปใน Chat โดยใช้ Apps Script และเหตุการณ์การโต้ตอบ คำแนะนำใน จัดการสิทธิ์ OAuth แบบละเอียดใน Apps Script จะใช้งานได้โดยมีข้อควรพิจารณา 1 ข้อดังนี้
ScriptApp.requireScopes
หยุดการเรียกใช้สคริปต์หากไม่ได้รับขอบเขตที่ระบุ
แต่ผู้ใช้จะเห็นการ์ดการกำหนดค่าใน Chat
แทนหน้าจอยินยอม OAuth การ์ดการกำหนดค่าจะแจ้งให้
ผู้ใช้ให้สิทธิ์ขอบเขตที่ขอทั้งหมดเสมอ แทนที่จะให้สิทธิ์เฉพาะขอบเขตที่ยังไม่ได้ให้
หากต้องการตรวจสอบการให้สิทธิ์ระดับขอบเขตแต่ละรายการ ให้ใช้
ScriptApp.getAuthorizationInfo
เพื่อตรวจสอบการให้สิทธิ์ และหากจำเป็น ให้ขอการให้สิทธิ์โดยใช้ข้อความส่วนตัว
ตัวอย่างต่อไปนี้แสดงวิธีตรวจสอบสิทธิ์ที่เฉพาะเจาะจง (เช่น การเข้าถึงปฏิทิน) และหากไม่มี ให้ส่งข้อความส่วนตัวพร้อม URL การให้สิทธิ์ที่จำเป็น
Apps Script
/**
* Responds to a MESSAGE event in Google Chat.
* Checks for required permissions and if missing asks for them.
*
* @param {Object} event the event object from Chat
* @return {Object} JSON response
*/
function onMessage(event) {
// Check if the script has the necessary permissions.
// In this example, the script checks for the "calendar.events" scope.
var requiredScopes = ['https://www.googleapis.com/auth/calendar.events'];
var authInfo = ScriptApp.getAuthorizationInfo(ScriptApp.AuthMode.FULL, requiredScopes);
// If permissions are missing, return a message with the authorization URL.
if (authInfo.getAuthorizationStatus() === ScriptApp.AuthorizationStatus.REQUIRED) {
var authUrl = authInfo.getAuthorizationUrl();
return {
"text": "This action requires authorization. Please <" + authUrl + "|click here to authorize>.",
"privateMessageViewer": {
"name": event.user.name
}
};
}
// Permission granted; proceed with the application logic.
// ...
}
ปลายทาง HTTP
หากคุณสร้างแอป Chat โดยใช้ปลายทาง HTTP แอป Chat ควรรองรับสิทธิ์ OAuth แบบละเอียด
ส่วนเสริมของ Google Workspace ที่ขยายการทำงานของ Chat
หากคุณสร้างแอป Chat เป็นส่วนเสริมของ Google Workspace (เช่น หากแอปขยายแอปอื่นๆ ของ Google Workspace เช่น Google ไดรฟ์หรือ Gmail) ให้กำหนดค่าไฟล์ Manifest และโค้ดเพื่อจัดการสิทธิ์ OAuth แบบละเอียด
ในไฟล์ Manifest ของส่วนเสริม ให้ตั้งค่าฟิลด์
granularOauthPermissionSupportเป็นOPT_INดูข้อมูลเพิ่มเติมเกี่ยวกับฟิลด์granularOauthPermissionSupportได้ที่ ย้ายข้อมูลไปยังโฟลว์สิทธิ์ OAuth แบบละเอียดJSON
{ "oauthScopes": [ "https://www.googleapis.com/auth/chat.messages", "https://www.googleapis.com/auth/calendar.events" ], "addOns": { "common": { "name": "My Chat App", "logoUrl": "https://lh3.googleusercontent.com/..." }, "chat": {}, "httpOptions": { "granularOauthPermissionSupport": "OPT_IN" } } }หากต้องการดูว่าผู้ใช้ให้สิทธิ์ขอบเขตใดไว้ ให้ตรวจสอบฟิลด์
authorizationEventObject.authorizedScopesในโค้ด หากไม่มีขอบเขตที่จำเป็น ให้ส่งคืนการดำเนินการrequesting_google_scopesเพื่อแจ้งให้ผู้ใช้ระบุขอบเขตที่ขาดหายไปNode.js
// Check for authorized scopes. const authorizedScopes = req.body.authorizationEventObject.authorizedScopes || []; if (!authorizedScopes.includes('https://www.googleapis.com/auth/chat.messages')) { // Respond with a request for the missing scope. res.send({ 'requesting_google_scopes': { 'scopes': ['https://www.googleapis.com/auth/chat.messages'] } }); return; }Python
from flask import jsonify, request # Check for authorized scopes. event_data = request.get_json() authorized_scopes = event_data.get('authorizationEventObject', {}).get('authorizedScopes', []) if 'https://www.googleapis.com/auth/chat.messages' not in authorized_scopes: # Respond with a request for the missing scope. return jsonify({ 'requesting_google_scopes': { 'scopes': ['https://www.googleapis.com/auth/chat.messages'] } })Java
import com.google.gson.JsonArray; import com.google.gson.JsonObject; import java.util.List; // Check for authorized scopes. List<String> authorizedScopes = event.getAuthorizationEventObject().getAuthorizedScopes(); if (!authorizedScopes.contains("https://www.googleapis.com/auth/chat.messages")) { // Respond with a request for the missing scope. JsonObject requestingGoogleScopes = new JsonObject(); JsonArray scopes = new JsonArray(); scopes.add("https://www.googleapis.com/auth/chat.messages"); requestingGoogleScopes.add("scopes", scopes); JsonObject response = new JsonObject(); response.add("requesting_google_scopes", requestingGoogleScopes); return response.toString(); }หากต้องการขอขอบเขตทั้งหมดที่เชื่อมโยงกับส่วนเสริม ให้ตั้งค่า
all_scopesเป็นtrueNode.js
res.send({ 'requesting_google_scopes': { 'all_scopes': true } });Python
from flask import jsonify return jsonify({ 'requesting_google_scopes': { 'all_scopes': True } })Java
import com.google.gson.JsonObject; JsonObject requestingGoogleScopes = new JsonObject(); requestingGoogleScopes.addProperty("all_scopes", true); JsonObject response = new JsonObject(); response.add("requesting_google_scopes", requestingGoogleScopes); return response.toString();
ดูวิธีการโดยละเอียดได้ที่จัดการสิทธิ์แบบละเอียดสำหรับส่วนเสริม HTTP ของ Google Workspace
แอปแชท HTTP แบบสแตนด์อโลน
หากแอป Chat เป็นบริการ HTTP แบบสแตนด์อโลน (ไม่ใช่ส่วนเสริมของ Google Workspace) คุณจะต้องจัดการโฟลว์ OAuth 2.0 ด้วยตนเอง
เมื่อเรียกโทเค็นที่จัดเก็บไว้หรือแลกรหัสการให้สิทธิ์ ให้ตรวจสอบว่ามีการให้ขอบเขตใดบ้าง หากไม่มีขอบเขตที่จำเป็น ให้แจ้งให้ผู้ใช้ให้สิทธิ์
Node.js
// 1. List authorized scopes.
const fs = require('fs');
const tokens = JSON.parse(fs.readFileSync('token.json'));
const grantedScopes = tokens.scope.split(' ');
// 2. Detect missing scopes.
const requiredScopes = ['https://www.googleapis.com/auth/chat.messages'];
const missingScopes = requiredScopes.filter(scope => !grantedScopes.includes(scope));
if (missingScopes.length > 0) {
// 3. Request missing scopes.
const authUrl = oauth2Client.generateAuthUrl({
access_type: 'offline',
scope: missingScopes,
include_granted_scopes: true
});
res.redirect(authUrl);
}
// To request all scopes instead of just the missing ones:
const allScopesAuthUrl = oauth2Client.generateAuthUrl({
access_type: 'offline',
scope: requiredScopes,
include_granted_scopes: true
});
Python
from flask import redirect
from google.oauth2.credentials import Credentials
# 1. List authorized scopes.
credentials = Credentials.from_authorized_user_file('token.json')
granted_scopes = set(credentials.scopes)
# 2. Detect missing scopes.
required_scopes = {'https://www.googleapis.com/auth/chat.messages'}
missing_scopes = required_scopes - granted_scopes
if missing_scopes:
# 3. Request missing scopes.
flow.scope = list(missing_scopes)
auth_url, _ = flow.authorization_url(
access_type='offline',
include_granted_scopes=True
)
return redirect(auth_url)
# To request all scopes instead of just the missing ones:
flow.scope = list(required_scopes)
all_scopes_auth_url, _ = flow.authorization_url(
access_type='offline',
include_granted_scopes='true'
)
Java
import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeRequestUrl;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
// 1. List authorized scopes.
// The "user" string is the user ID for which to load credentials.
Credential credential = flow.loadCredential("user");
Collection<String> grantedScopes = credential.getScopes();
// 2. Detect missing scopes.
// The `requiredScopes` variable contains a list of the OAuth scopes
// that your app requires to function. Define this variable with the
// scopes needed by your application.
List<String> requiredScopes = Arrays.asList("https://www.googleapis.com/auth/chat.messages");
List<String> missingScopes = new ArrayList<>();
for (String scope : requiredScopes) {
if (!grantedScopes.contains(scope)) {
missingScopes.add(scope);
}
}
if (!missingScopes.isEmpty()) {
// 3. Request missing scopes.
GoogleAuthorizationCodeRequestUrl urlBuilder = new GoogleAuthorizationCodeRequestUrl(
clientId, redirectUri, missingScopes)
.setAccessType("offline")
.set("include_granted_scopes", "true");
String authUrl = urlBuilder.build();
response.sendRedirect(authUrl);
}
// To request all scopes instead of just the missing ones:
GoogleAuthorizationCodeRequestUrl allScopesUrlBuilder = new GoogleAuthorizationCodeRequestUrl(
clientId, redirectUri, requiredScopes)
.setAccessType("offline")
.set("include_granted_scopes", "true");
String allScopesAuthUrl = allScopesUrlBuilder.build();
ดูข้อมูลเพิ่มเติมได้ที่ สิทธิ์ OAuth แบบละเอียด
หัวข้อที่เกี่ยวข้อง
- ดูภาพรวมของการตรวจสอบสิทธิ์และการให้สิทธิ์ใน Google Chat ได้ที่ ดูข้อมูลเกี่ยวกับการตรวจสอบสิทธิ์และการให้สิทธิ์
- หากต้องการตั้งค่าการตรวจสอบสิทธิ์ผู้ใช้ โปรดดูตรวจสอบสิทธิ์และให้สิทธิ์ในฐานะผู้ใช้ Google Chat
- หากต้องการความช่วยเหลือในการตั้งค่าสิทธิ์ OAuth แบบละเอียดใน Apps Script หรือส่วนเสริม HTTP ของ Google Workspace โปรดดูข้อมูลต่อไปนี้
- ดูข้อมูลเพิ่มเติมเกี่ยวกับสิทธิ์ OAuth แบบละเอียดได้ที่ สิทธิ์ OAuth แบบละเอียด