उपयोगकर्ता की पहचान की पुष्टि करने वाले Chat ऐप्लिकेशन में, OAuth की अलग-अलग अनुमतियां देने की सुविधा होनी चाहिए. इससे उपयोगकर्ता, अनुरोध किए गए दायरों में से कुछ को ही अनुमति दे पाएंगे. उदाहरण के लिए, कोई उपयोगकर्ता अपने नाम को ऐक्सेस करने की अनुमति दे सकता है, लेकिन अपने कैलेंडर को ऐक्सेस करने की अनुमति नहीं दे सकता.
OAuth की अलग-अलग अनुमतियों को मैनेज करने का तरीका, इस बात पर निर्भर करता है कि आपने अपना Chat ऐप्लिकेशन कैसे बनाया है:
- Apps Script का इस्तेमाल करके बनाए गए Google Workspace ऐड-ऑन, जो Chat की सुविधाओं को बढ़ाते हैं
- Apps Script का इस्तेमाल करके बनाए गए स्टैंडअलोन Chat ऐप्लिकेशन
- एचटीटीपी का इस्तेमाल करके बनाए गए Google Workspace ऐड-ऑन, जो Chat की सुविधाओं को बढ़ाते हैं
- एचटीटीपी का इस्तेमाल करके बनाए गए स्टैंडअलोन Chat ऐप्लिकेशन
Apps Script
अगर आपने Apps Script का इस्तेमाल करके अपना Chat ऐप्लिकेशन बनाया है, तो Apps Script, OAuth की अलग-अलग अनुमतियों को अपने-आप मैनेज करता है. हालांकि, पक्का करें कि आपका कोड उन स्थितियों को मैनेज करे जिनमें कोई उपयोगकर्ता, अनुरोध किए गए सभी दायरों को अनुमति नहीं देता है. यह तरीका इस बात पर निर्भर करता है कि आपकी Apps Script, Google Workspace का ऐसा ऐड-ऑन है जो Apps Script का इस्तेमाल करके Google Chat की सुविधाओं को बढ़ाता है या Apps Script और इंटरैक्शन इवेंट की मदद से बनाया गया स्टैंडअलोन Chat ऐप्लिकेशन है.
Google Workspace ऐड-ऑन, जो Chat की सुविधाओं को बढ़ाते हैं
अगर आपने Apps Script का इस्तेमाल करके, Google Workspace के ऐसे ऐड-ऑन के तौर पर अपना Chat ऐप्लिकेशन बनाया है जो Google Chat की सुविधाओं को बढ़ाता है, तो Apps Script में OAuth की अलग-अलग अनुमतियों को मैनेज करना में दिए गए निर्देशों का पालन करें.
Apps Script का इस्तेमाल करके बनाए गए स्टैंडअलोन Chat ऐप्लिकेशन
अगर आपने Apps Script और इंटरैक्शन इवेंटका इस्तेमाल करके अपना Chat ऐप्लिकेशन बनाया है, तो Apps Script में OAuth की अलग-अलग अनुमतियों को मैनेज करना में दिए गए निर्देश, इस बात को ध्यान में रखते हुए काम करते हैं:
ScriptApp.requireScopes
अगर बताई गई अनुमतियां नहीं दी जाती हैं, तो स्क्रिप्ट को चलने से रोक देता है. हालांकि, उपयोगकर्ता को OAuth की सहमति वाली स्क्रीन के बजाय, Chat में कॉन्फ़िगरेशन कार्ड दिखता है. कॉन्फ़िगरेशन कार्ड हमेशा उपयोगकर्ता से, सिर्फ़ उन अनुमतियों के बजाय अनुरोध की गई सभी अनुमतियों को देने के लिए कहता है जिन्हें अनुमति नहीं दी गई है.
अनुमति के दायरे के हिसाब से अलग-अलग जांच करने के लिए,
ScriptApp.getAuthorizationInfo
का इस्तेमाल करके, अनुमति की जांच करें. अगर ज़रूरी हो, तो निजी मैसेज का इस्तेमाल करके अनुमति का अनुरोध करें.
यहां दिए गए उदाहरण में, किसी खास अनुमति (जैसे, कैलेंडर को ऐक्सेस करने की अनुमति) की जांच करने का तरीका बताया गया है. अगर अनुमति नहीं मिली है, तो ज़रूरी अनुमति वाले यूआरएल के साथ एक निजी मैसेज भेजें.
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.
// ...
}
एचटीटीपी एंडपॉइंट
अगर आपने एचटीटीपी एंडपॉइंट का इस्तेमाल करके अपना Chat ऐप्लिकेशन बनाया है, तो आपके Chat ऐप्लिकेशन में OAuth की अलग-अलग अनुमतियां देने की सुविधा होनी चाहिए.
Google Workspace ऐड-ऑन, जो Chat की सुविधाओं को बढ़ाते हैं
अगर आपने अपना Chat ऐप्लिकेशन, Google Workspace के ऐड-ऑन के तौर पर बनाया है, तो OAuth की अलग-अलग अनुमतियों को मैनेज करने के लिए, अपने कोड को कॉन्फ़िगर करें. देखें कि उपयोगकर्ता ने अनुमति के किन दायरों को अनुमति दी है. अगर ज़रूरी हो, तो उन दायरों या सभी दायरों के लिए अनुमति का अनुरोध करें जिनके लिए अनुमति नहीं मिली है.
अपने ऐड-ऑन की मेनिफ़ेस्ट फ़ाइल में, अनुमति के ज़रूरी दायरे
oauthScopesफ़ील्ड में तय करें. यह फ़ील्ड,projects.deploymentsसंसाधन का हिस्सा है.यहां दिए गए उदाहरण में, अनुमति के
chat.messagesऔरcalendar.eventsदायरों की ज़रूरत होती है: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": {}, "calendar": {}, "httpOptions": {} } }यह देखने के लिए कि उपयोगकर्ता ने किन दायरों को अनुमति दी है,
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() != null ? event.getAuthorizationEventObject().getAuthorizedScopes() : null; if (authorizedScopes == null || !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कोtrueपर सेट करें:Node.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();
ज़्यादा जानकारी के लिए, एचटीटीपी का इस्तेमाल करके बनाए गए Google Workspace ऐड-ऑन के लिए, अलग-अलग अनुमतियां मैनेज करना लेख पढ़ें.
एचटीटीपी का इस्तेमाल करके बनाए गए स्टैंडअलोन Chat ऐप्लिकेशन
अगर आपका Chat ऐप्लिकेशन, एचटीटीपी का इस्तेमाल करके बनाई गई स्टैंडअलोन सेवा है (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 के उपयोगकर्ता के तौर पर पहचान की पुष्टि करना और अनुमति देना देखें.
- Apps Script में OAuth की अलग-अलग अनुमतियां सेट अप करने या एचटीटीपी का इस्तेमाल करके बनाए गए Google Workspace ऐड-ऑन के लिए, ये लेख पढ़ें:
- OAuth की अलग-अलग अनुमतियों के बारे में ज़्यादा जानने के लिए, OAuth की अलग-अलग अनुमतियां देखें.