ব্যবহারকারী প্রমাণীকরণ ব্যবহার করে এমন চ্যাট অ্যাপগুলিকে অবশ্যই গ্রানুলার OAuth অনুমতি সমর্থন করতে হবে যাতে ব্যবহারকারীরা অনুরোধকৃত স্কোপের একটি উপসেট মঞ্জুর করতে পারেন। উদাহরণস্বরূপ, একজন ব্যবহারকারী তাদের নামের অ্যাক্সেস দিতে পারেন কিন্তু তাদের ক্যালেন্ডারে অ্যাক্সেস প্রত্যাখ্যান করতে পারেন।
আপনি কীভাবে আপনার চ্যাট অ্যাপ তৈরি করেন তার উপর নির্ভর করে গ্রানুলার OAuth অনুমতিগুলি পরিচালনা করা:
- অ্যাপস স্ক্রিপ্ট গুগল ওয়ার্কস্পেস অ্যাড-অন যা চ্যাটকে প্রসারিত করে
- স্বতন্ত্র অ্যাপস স্ক্রিপ্ট চ্যাট অ্যাপস
- HTTP Google Workspace অ্যাড-অন যা Chat প্রসারিত করে
- স্বতন্ত্র HTTP চ্যাট অ্যাপস
অ্যাপস স্ক্রিপ্ট
আপনি যদি Apps Script ব্যবহার করে আপনার Chat অ্যাপ তৈরি করেন, তাহলে Apps Script স্বয়ংক্রিয়ভাবে গ্রানুলার OAuth অনুমতিগুলি পরিচালনা করে। তবে, নিশ্চিত করুন যে আপনার কোড এমন ক্ষেত্রে পরিচালনা করে যেখানে কোনও ব্যবহারকারী সমস্ত অনুরোধকৃত স্কোপ মঞ্জুর করেন না। পদ্ধতিটি নির্ভর করে আপনার Apps Script একটি Google Workspace অ্যাড-অন যা Apps Script ব্যবহার করে Google Chat প্রসারিত করে, নাকি Apps Script এবং ইন্টারঅ্যাকশন ইভেন্ট দিয়ে তৈরি একটি স্বতন্ত্র Chat অ্যাপ তার উপর।
Google Workspace অ্যাড-অন যা Chat-কে আরও উন্নত করে
যদি আপনি আপনার চ্যাট অ্যাপটিকে একটি Google Workspace অ্যাড-অন হিসেবে তৈরি করেন যা Apps Script ব্যবহার করে Google Chat প্রসারিত করে , তাহলে Apps Script-এ Handle granular OAuth permissions-এর নির্দেশাবলী অনুসরণ করুন।
স্বতন্ত্র অ্যাপস স্ক্রিপ্ট চ্যাট অ্যাপস
আপনি যদি Apps Script এবং ইন্টারঅ্যাকশন ইভেন্ট ব্যবহার করে আপনার Chat অ্যাপ তৈরি করেন, তাহলে Apps Script-এ Handle granular OAuth permissions-এর নির্দেশাবলী একটি বিষয় বিবেচনা করে কাজ করে:
নির্দিষ্ট স্কোপগুলি মঞ্জুর না করা হলে ScriptApp.requireScopes স্ক্রিপ্ট এক্সিকিউশন বন্ধ করে দেয়, কিন্তু ব্যবহারকারী OAuth কনসেন্ট স্ক্রিনের পরিবর্তে Chat-এ একটি কনফিগারেশন কার্ড দেখতে পান। কনফিগারেশন কার্ডটি সর্বদা ব্যবহারকারীকে কেবল অমঞ্জুরিপ্রাপ্ত স্কোপগুলির পরিবর্তে সমস্ত অনুরোধ করা স্কোপগুলি মঞ্জুর করতে অনুরোধ করে।
ব্যক্তিগত অনুমোদনের সুযোগ-স্তরের পরীক্ষা প্রদানের জন্য, অনুমোদন পরীক্ষা করার জন্য ScriptApp.getAuthorizationInfo ব্যবহার করুন এবং প্রয়োজনে একটি ব্যক্তিগত বার্তা ব্যবহার করে অনুমোদনের অনুরোধ করুন।
নিম্নলিখিত উদাহরণে দেখানো হয়েছে কিভাবে একটি নির্দিষ্ট অনুমতি (যেমন ক্যালেন্ডার অ্যাক্সেস) পরীক্ষা করতে হয় এবং যদি অনুপস্থিত থাকে, তাহলে প্রয়োজনীয় অনুমোদনের URL সহ একটি ব্যক্তিগত বার্তা পাঠান।
অ্যাপস স্ক্রিপ্ট
/**
* 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 এন্ডপয়েন্ট
যদি আপনি HTTP এন্ডপয়েন্ট ব্যবহার করে আপনার চ্যাট অ্যাপ তৈরি করেন, তাহলে আপনার চ্যাট অ্যাপটি গ্রানুলার OAuth অনুমতি সমর্থন করবে।
Google Workspace অ্যাড-অন যা Chat-কে আরও উন্নত করে
যদি আপনি আপনার চ্যাট অ্যাপটিকে Google Workspace অ্যাড-অন হিসেবে তৈরি করেন (উদাহরণস্বরূপ, যদি এটি Google Drive বা Gmail এর মতো অন্যান্য Google Workspace অ্যাপগুলিকে প্রসারিত করে), তাহলে আপনার ম্যানিফেস্ট ফাইল এবং কোডটি এমনভাবে কনফিগার করুন যাতে গ্রানুলার OAuth অনুমতিগুলি পরিচালনা করা যায়:
আপনার অ্যাড-অনের ম্যানিফেস্ট ফাইলে ,
granularOauthPermissionSupportফিল্ডটিOPT_INএ সেট করুন।granularOauthPermissionSupportফিল্ড সম্পর্কে আরও জানতে, granular 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অ্যাকশন ফেরত দিন।নোড.জেএস
// 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; }পাইথন
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'] } })জাভা
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_scopestrueএ সেট করুন:নোড.জেএস
res.send({ 'requesting_google_scopes': { 'all_scopes': true } });পাইথন
from flask import jsonify return jsonify({ 'requesting_google_scopes': { 'all_scopes': True } })জাভা
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 চ্যাট অ্যাপস
যদি আপনার চ্যাট অ্যাপটি একটি স্বতন্ত্র HTTP পরিষেবা হয় (কোনও Google Workspace অ্যাড-অন নয়), তাহলে আপনি নিজেই OAuth 2.0 ফ্লো পরিচালনা করবেন।
যখন আপনি একটি সংরক্ষিত টোকেন পুনরুদ্ধার করেন বা একটি অনুমোদন কোড বিনিময় করেন, তখন কোন স্কোপগুলি মঞ্জুর করা হয়েছে তা পরীক্ষা করুন। যদি প্রয়োজনীয় স্কোপগুলি অনুপস্থিত থাকে, তাহলে ব্যবহারকারীকে সেগুলি অনুমোদন করতে বলুন।
নোড.জেএস
// 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
});
পাইথন
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'
)
জাভা
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 ব্যবহারকারী হিসেবে প্রমাণীকরণ এবং অনুমোদন করুন" দেখুন।
- অ্যাপস স্ক্রিপ্টে অথবা HTTP গুগল ওয়ার্কস্পেস অ্যাড-অনগুলিতে গ্রানুলার OAuth অনুমতি সেট আপ করতে সাহায্যের জন্য, দেখুন:
- গ্রানুলার OAuth অনুমতি সম্পর্কে আরও জানতে, গ্রানুলার OAuth অনুমতি দেখুন।