ব্যবহারকারীর যাচাইকরণ ব্যবহার করে এমন চ্যাট অ্যাপকে অবশ্যই গ্র্যানুলার OAuth অনুমতি সাপোর্ট করতে হবে যাতে ব্যবহারকারী অনুরোধ করা স্কোপের সাবসেট মঞ্জুর করতে পারেন। যেমন, কোনও ব্যবহারকারী তার নাম অ্যাক্সেস করার অনুমতি দিতে পারেন, কিন্তু তার ক্যালেন্ডার অ্যাক্সেস করার অনুমতি নাও দিতে পারেন।
আপনি কীভাবে নিজের চ্যাট অ্যাপ তৈরি করেছেন তার উপর নির্ভর করে গ্র্যানুলার OAuth অনুমতি ম্যানেজ করা হয়:
Apps Script
আপনি যদি Apps Script ব্যবহার করে আপনার Chat অ্যাপ তৈরি করেন, তাহলে Apps Script অটোমেটিক গ্র্যানুলার OAuth অনুমতি ম্যানেজ করে। তবে, ব্যবহারকারী অনুরোধ করা সব স্কোপে অ্যাক্সেস না দিলে আপনার কোড যেন তা ম্যানেজ করতে পারে তা নিশ্চিত করুন। Apps Script-এ গ্র্যানুলার OAuth অনুমতি ম্যানেজ করুন নিবন্ধে দেওয়া নির্দেশাবলী অনুসরণ করুন।
HTTP এন্ডপয়েন্ট
আপনি যদি HTTP এন্ডপয়েন্ট ব্যবহার করে আপনার Chat অ্যাপ তৈরি করেন, তাহলে গ্র্যানুলার OAuth অনুমতি ম্যানেজ করার জন্য আপনার কোড কনফিগার করুন। ব্যবহারকারী কোন কোন অনুমোদনের স্কোপে অনুমতি দিয়েছেন তা চেক করুন এবং প্রয়োজন হলে, যেসব স্কোপ নেই বা সব স্কোপের জন্য অনুমতির অনুরোধ করুন।
আপনার Chat অ্যাপের ম্যানিফেস্ট ফাইলে,
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(); }Chat অ্যাপের সাথে যুক্ত সব স্কোপের অনুরোধ করতে,
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();
বিস্তারিত নির্দেশাবলীর জন্য, HTTP Google Workspace অ্যাড-অনের জন্য গ্র্যানুলার অনুমতি ম্যানেজ করুন দেখুন।
সম্পর্কিত বিষয়
- Google Chat-এ যাচাইকরণ ও অনুমোদন সম্পর্কে এক ঝলকে জানতে, যাচাইকরণ ও অনুমোদন সম্পর্কে জানুন লিঙ্কে দেখুন।
- ব্যবহারকারী যাচাইকরণ সেট-আপ করতে, Google Chat ব্যবহারকারী হিসেবে যাচাই ও অনুমোদন করুন দেখুন।
- Apps Script-এ বা HTTP Chat অ্যাপের জন্য গ্র্যানুলার OAuth অনুমতি সেট-আপ করার ব্যাপারে সাহায্য পেতে, এগুলি দেখুন:
- গ্র্যানুলার OAuth অনুমতি সম্পর্কে আরও জানতে, গ্র্যানুলার OAuth অনুমতি দেখুন।
অ্যাড-অন নয় এমন চ্যাট অ্যাপ: Google Chat অ্যাপের জন্য গ্র্যানুলার OAuth অনুমতি ম্যানেজ করা
আপনি যদি Google Workspace অ্যাড-অন নয় এমন কোনও Chat অ্যাপ ম্যানেজ করেন, তাহলে গ্র্যানুলার OAuth অনুমতি ম্যানেজ করতে নিম্নলিখিত নির্দেশাবলী ব্যবহার করুন।
অ্যাড-অন নয় এমন Apps স্ক্রিপ্ট Chat অ্যাপ
আপনি যদি Apps Script ব্যবহার করে এমন একটি Chat অ্যাপ তৈরি করে থাকেন যা কোনও অ্যাড-অন নয়, তাহলে Apps Script-এ গ্র্যানুলার OAuth অনুমতি ম্যানেজ করুন লিঙ্কে দেওয়া নির্দেশাবলী একটি বিষয় বিবেচনা করে কাজ করে:
ScriptApp.requireScopes
নির্দিষ্ট স্কোপ
অনুমোদিত না হলে স্ক্রিপ্ট এক্সিকিউশন বন্ধ করে দেয়, কিন্তু OAuth সম্মতি স্ক্রিনের
পরিবর্তে ব্যবহারকারী Chat-এ কনফিগারেশন কার্ড দেখতে পান। কনফিগারেশন কার্ড সবসময় ব্যবহারকারীকে
অনুরোধ করা সব স্কোপে অ্যাক্সেস দিতে প্রম্পট করে, শুধু যেগুলিতে অ্যাক্সেস দেওয়া হয়নি সেগুলিতে নয়।
আলাদা আলাদা অনুমোদনের স্কোপ-লেভেল চেক প্রদান করতে, অনুমোদনের জন্য চেক করতে এবং প্রয়োজন হলে
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 পরিষেবা হলে, 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();