Google Workspace ऐड-ऑन का इस्तेमाल करके, Google से बाहर की सेवाओं से कनेक्ट करना

आपका Google Workspace ऐड-ऑन प्रोजेक्ट, Apps Script की पहले से मौजूद और बेहतर सेवाओं की मदद से, Google के कई प्रॉडक्ट को सीधे तौर पर कनेक्ट कर सकता है.

Google से बाहर के एपीआई और सेवाओं को भी ऐक्सेस किया जा सकता है. अगर इस सेवा के लिए अनुमति की ज़रूरत नहीं है, तो आप आम तौर पर सही UrlFetch अनुरोध कर सकते हैं और फिर ऐड-ऑन से जवाब को समझने के लिए कह सकते हैं.

हालांकि, अगर Google से बाहर की किसी सेवा के लिए अनुमति की ज़रूरत होती है, तो आपको उस सेवा के लिए OAuth को कॉन्फ़िगर करना होगा. Apps Script के लिए OAuth2 लाइब्रेरी का इस्तेमाल करके, इस प्रोसेस को आसान बनाया जा सकता है. OAuth1 वर्शन भी उपलब्ध है.

OAuth सेवा का इस्तेमाल करना

जब Google से बाहर की किसी सेवा से कनेक्ट करने के लिए OAuth सेवा ऑब्जेक्ट का इस्तेमाल किया जाता है, तो आपके Google Workspace ऐड-ऑन को यह पता लगाना होता है कि अनुमति की ज़रूरत कब होगी. साथ ही, अनुमति मिलने पर ही ऑथराइज़ेशन फ़्लो शुरू करें.

अनुमति देने के फ़्लो में नीचे दी गई चीज़ें शामिल होती हैं:

  1. उपयोगकर्ता को सूचना देना कि पुष्टि करने की ज़रूरत है. साथ ही, प्रोसेस शुरू करने के लिए लिंक देकर.
  2. Google के अलावा, किसी दूसरी सेवा से अनुमति लेना.
  3. सुरक्षित संसाधन को फिर से ऐक्सेस करने के लिए, ऐड-ऑन को रीफ़्रेश किया जा रहा है.

जब Google से अनुमति मिलने की ज़रूरत न हो, तब Google Workspace ऐड-ऑन इन्फ़्रास्ट्रक्चर इस जानकारी को मैनेज करता है. आपके ऐड-ऑन को सिर्फ़ यह पता लगाना होता है कि अनुमति की ज़रूरत कब है. साथ ही, ज़रूरत होने पर ही ऑथराइज़ेशन फ़्लो को शुरू करना होता है.

हमें पता चला है कि अनुमति देने की ज़रूरत है

हो सकता है कि किसी अनुरोध में कई वजहों से किसी सुरक्षित संसाधन को ऐक्सेस करने की अनुमति न हो, जैसे:

  • ऐक्सेस टोकन अभी तक जनरेट नहीं किया गया है या इसकी समयसीमा खत्म हो गई है.
  • ऐक्सेस टोकन में वह संसाधन शामिल नहीं है जिसका अनुरोध किया गया था.
  • ऐक्सेस टोकन में अनुरोध के ज़रूरी दायरे शामिल नहीं हैं.

आपके ऐड-ऑन कोड को इन मामलों का पता लगाना चाहिए. OAuth लाइब्रेरी hasAccess() फ़ंक्शन से आपको पता चल सकता है कि फ़िलहाल आपके पास किसी सेवा का ऐक्सेस है या नहीं. इसके अलावा, UrlFetchApp fetch() अनुरोधों का इस्तेमाल करते समय, muteHttpExceptions पैरामीटर को true पर सेट किया जा सकता है. यह अनुरोध को पूरा न करने पर, अनुरोध को अपवाद के तौर पर दिखाता है. साथ ही, आपको वापस आए HttpResponse ऑब्जेक्ट में, अनुरोध के रिस्पॉन्स कोड और कॉन्टेंट की जांच करने की सुविधा मिलती है.

जब ऐड-ऑन को यह पता चलता है कि अनुमति की ज़रूरत है, तो यह ऑथराइज़ेशन फ़्लो ट्रिगर करता है.

अनुमति देने के फ़्लो को शुरू करना

AuthorizationException ऑब्जेक्ट बनाने, उसकी प्रॉपर्टी सेट करने, और throwException() फ़ंक्शन को कॉल करने के लिए, कार्ड सेवा का इस्तेमाल करके ऑथराइज़ेशन फ़्लो को शुरू किया जाता है. अपवाद डालने से पहले, आपको यह जानकारी देनी होगी:

  1. ज़रूरी है. अनुमति देने वाला यूआरएल. इसकी जानकारी, Google की बाहर की सेवा देती है और अनुमति देने की प्रोसेस शुरू होने पर उपयोगकर्ता को किस जगह पर भेजा जाता है. आपने यह यूआरएल setAuthorizationUrl() फ़ंक्शन का इस्तेमाल करके सेट किया है.
  2. ज़रूरी है. संसाधन के डिसप्ले नेम वाली स्ट्रिंग. जब अनुमति का अनुरोध किया जाता है, तब उपयोगकर्ता के लिए संसाधन की पहचान करता है. आपने यह नाम setResourceDisplayName() फ़ंक्शन का इस्तेमाल करके सेट किया है.
  3. उस कॉलबैक फ़ंक्शन का नाम जो पसंद के मुताबिक अनुमति देने का प्रॉम्प्ट बनाता है. यह कॉलबैक, बनाए गए Card ऑब्जेक्ट का ऐरे बनाता है, जो ऑथराइज़ेशन को मैनेज करने के लिए, यूज़र इंटरफ़ेस (यूआई) कम्पोज़ करता है. यह वैकल्पिक है; अगर सेट नहीं किया जाता है, तो डिफ़ॉल्ट ऑथराइज़ेशन कार्ड का इस्तेमाल किया जाता है. आपने setCustomUiCallback() फ़ंक्शन का इस्तेमाल करके, कॉलबैक फ़ंक्शन सेट किया है.

Google प्लैटफ़ॉर्म से बाहर के OAuth कॉन्फ़िगरेशन का उदाहरण

इस कोड सैंपल में बताया गया है कि बिना Google API वाले OAuth का इस्तेमाल करने के लिए, ऐड-ऑन को कैसे कॉन्फ़िगर किया जाए. यह एपीआई को ऐक्सेस करने के लिए एक सेवा बनाने के लिए, Apps Script के लिए OAuth2 का इस्तेमाल करता है.

/**
 * Attempts to access a non-Google API using a constructed service
 * object.
 *
 * If your add-on needs access to non-Google APIs that require OAuth,
 * you need to implement this method. You can use the OAuth1 and
 * OAuth2 Apps Script libraries to help implement it.
 *
 * @param {String} url         The URL to access.
 * @param {String} method_opt  The HTTP method. Defaults to GET.
 * @param {Object} headers_opt The HTTP headers. Defaults to an empty
 *                             object. The Authorization field is added
 *                             to the headers in this method.
 * @return {HttpResponse} the result from the UrlFetchApp.fetch() call.
 */
function accessProtectedResource(url, method_opt, headers_opt) {
  var service = getOAuthService();
  var maybeAuthorized = service.hasAccess();
  if (maybeAuthorized) {
    // A token is present, but it may be expired or invalid. Make a
    // request and check the response code to be sure.

    // Make the UrlFetch request and return the result.
    var accessToken = service.getAccessToken();
    var method = method_opt || 'get';
    var headers = headers_opt || {};
    headers['Authorization'] =
        Utilities.formatString('Bearer %s', accessToken);
    var resp = UrlFetchApp.fetch(url, {
      'headers': headers,
      'method' : method,
      'muteHttpExceptions': true, // Prevents thrown HTTP exceptions.
    });

    var code = resp.getResponseCode();
    if (code >= 200 && code < 300) {
      return resp.getContentText("utf-8"); // Success
    } else if (code == 401 || code == 403) {
       // Not fully authorized for this action.
       maybeAuthorized = false;
    } else {
       // Handle other response codes by logging them and throwing an
       // exception.
       console.error("Backend server error (%s): %s", code.toString(),
                     resp.getContentText("utf-8"));
       throw ("Backend server error: " + code);
    }
  }

  if (!maybeAuthorized) {
    // Invoke the authorization flow using the default authorization
    // prompt card.
    CardService.newAuthorizationException()
        .setAuthorizationUrl(service.getAuthorizationUrl())
        .setResourceDisplayName("Display name to show to the user")
        .throwException();
  }
}

/**
 * Create a new OAuth service to facilitate accessing an API.
 * This example assumes there is a single service that the add-on needs to
 * access. Its name is used when persisting the authorized token, so ensure
 * it is unique within the scope of the property store. You must set the
 * client secret and client ID, which are obtained when registering your
 * add-on with the API.
 *
 * See the Apps Script OAuth2 Library documentation for more
 * information:
 *   https://github.com/googlesamples/apps-script-oauth2#1-create-the-oauth2-service
 *
 *  @return A configured OAuth2 service object.
 */
function getOAuthService() {
  return OAuth2.createService('SERVICE_NAME')
      .setAuthorizationBaseUrl('SERVICE_AUTH_URL')
      .setTokenUrl('SERVICE_AUTH_TOKEN_URL')
      .setClientId('CLIENT_ID')
      .setClientSecret('CLIENT_SECRET')
      .setScope('SERVICE_SCOPE_REQUESTS')
      .setCallbackFunction('authCallback')
      .setCache(CacheService.getUserCache())
      .setPropertyStore(PropertiesService.getUserProperties());
}

/**
 * Boilerplate code to determine if a request is authorized and returns
 * a corresponding HTML message. When the user completes the OAuth2 flow
 * on the service provider's website, this function is invoked from the
 * service. In order for authorization to succeed you must make sure that
 * the service knows how to call this function by setting the correct
 * redirect URL.
 *
 * The redirect URL to enter is:
 * https://script.google.com/macros/d/<Apps Script ID>/usercallback
 *
 * See the Apps Script OAuth2 Library documentation for more
 * information:
 *   https://github.com/googlesamples/apps-script-oauth2#1-create-the-oauth2-service
 *
 *  @param {Object} callbackRequest The request data received from the
 *                  callback function. Pass it to the service's
 *                  handleCallback() method to complete the
 *                  authorization process.
 *  @return {HtmlOutput} a success or denied HTML message to display to
 *          the user. Also sets a timer to close the window
 *          automatically.
 */
function authCallback(callbackRequest) {
  var authorized = getOAuthService().handleCallback(callbackRequest);
  if (authorized) {
    return HtmlService.createHtmlOutput(
      'Success! <script>setTimeout(function() { top.window.close() }, 1);</script>');
  } else {
    return HtmlService.createHtmlOutput('Denied');
  }
}

/**
 * Unauthorizes the non-Google service. This is useful for OAuth
 * development/testing.  Run this method (Run > resetOAuth in the script
 * editor) to reset OAuth to re-prompt the user for OAuth.
 */
function resetOAuth() {
  getOAuthService().reset();
}

पसंद के मुताबिक अनुमति देने का अनुरोध बनाना

Google से बाहर की सेवा के लिए अनुमति देने वाला कार्ड

डिफ़ॉल्ट रूप से, अनुमति देने वाले प्रॉम्प्ट में कोई ब्रैंडिंग नहीं होती है. यह सिर्फ़ डिसप्ले नेम वाली स्ट्रिंग का इस्तेमाल करके बताता है कि ऐड-ऑन किस रिसॉर्स को ऐक्सेस करने की कोशिश कर रहा है. हालांकि, आपका ऐड-ऑन एक पसंद के मुताबिक बनाया गया ऑथराइज़ेशन कार्ड बना सकता है, जो इसी मकसद से काम करता है और उसमें अतिरिक्त जानकारी और ब्रैंडिंग शामिल हो सकती है.

ऐसे कस्टम यूज़र इंटरफ़ेस (यूआई) कॉलबैक फ़ंक्शन को लागू करके कस्टम प्रॉम्प्ट बनाया जाता है जो बनाए गए Card ऑब्जेक्ट का ऐरे दिखाता है. इस कलेक्शन में सिर्फ़ एक कार्ड होना चाहिए. अगर ज़्यादा जानकारी दी जाती है, तो उसके हेडर एक सूची में दिखाए जाते हैं. इससे उपयोगकर्ता को भ्रम की स्थिति पैदा हो सकती है.

लौटाए गए कार्ड को नीचे दिए गए काम करने होंगे:

  • उपयोगकर्ता को साफ़ तौर पर बताएं कि ऐड-ऑन उनकी ओर से Google से बाहर की किसी सेवा को ऐक्सेस करने की अनुमति मांग रहा है.
  • यह साफ़ तौर पर बताएं कि अनुमति मिलने पर ऐड-ऑन क्या कर सकता है.
  • उपयोगकर्ता को सेवा के अनुमति देने वाले यूआरएल पर ले जाने वाला बटन या उसके जैसा विजेट न हो. पक्का करें कि उपयोगकर्ता को इस विजेट का फ़ंक्शन साफ़ तौर पर समझ में आ जाए.
  • ऊपर दिए गए विजेट को OnClose.RELOAD_ADD_ON अपने OpenLink ऑब्जेक्ट की सेटिंग का इस्तेमाल करना चाहिए, ताकि अनुमति मिलने के बाद ऐड-ऑन को फिर से लोड किया जा सके.
  • अनुमति देने के अनुरोध से खोले गए सभी लिंक में एचटीटीपीएस इस्तेमाल करना ज़रूरी है.

कार्ड इस्तेमाल करने के लिए, ऑथराइज़ेशन फ़्लो को आपके AuthorizationException ऑब्जेक्ट पर मौजूद setCustomUiCallback() फ़ंक्शन को कॉल करना होता है.

इस उदाहरण में, कस्टम ऑथराइज़ेशन प्रॉम्प्ट के कॉलबैक फ़ंक्शन को दिखाया गया है:

/**
 * Returns an array of cards that comprise the customized authorization
 * prompt. Includes a button that opens the proper authorization link
 * for a non-Google service.
 *
 * When creating the text button, using the
 * setOnClose(CardService.OnClose.RELOAD_ADD_ON) function forces the add-on
 * to refresh once the authorization flow completes.
 *
 * @return {Card[]} The card representing the custom authorization prompt.
 */
function create3PAuthorizationUi() {
  var service = getOAuthService();
  var authUrl = service.getAuthorizationUrl();
  var authButton = CardService.newTextButton()
      .setText('Begin Authorization')
      .setAuthorizationAction(CardService.newAuthorizationAction()
          .setAuthorizationUrl(authUrl));

  var promptText =
      'To show you information from your 3P account that is relevant' +
      ' to the recipients of the email, this add-on needs authorization' +
      ' to: <ul><li>Read recipients of the email</li>' +
      '         <li>Read contact information from 3P account</li></ul>.';

  var card = CardService.newCardBuilder()
      .setHeader(CardService.newCardHeader()
          .setTitle('Authorization Required'))
      .addSection(CardService.newCardSection()
          .setHeader('This add-on needs access to your 3P account.')
          .addWidget(CardService.newTextParagraph()
              .setText(promptText))
          .addWidget(CardService.newButtonSet()
              .addButton(authButton)))
      .build();
  return [card];
}

/**
 * When connecting to the non-Google service, pass the name of the
 * custom UI callback function to the AuthorizationException object
 */
function accessProtectedResource(url, method_opt, headers_opt) {
  var service = getOAuthService();
  if (service.hasAccess()) {
    // Make the UrlFetch request and return the result.
    // ...
  } else {
    // Invoke the authorization flow using a custom authorization
    // prompt card.
    CardService.newAuthorizationException()
        .setAuthorizationUrl(service.getAuthorizationUrl())
        .setResourceDisplayName("Display name to show to the user")
        .setCustomUiCallback('create3PAuthorizationUi')
        .throwException();
  }
}

Google Workspace के सभी ऐप्लिकेशन में तीसरे पक्ष के लॉगिन क्रेडेंशियल को मैनेज करना

Google Workspace ऐड-ऑन का एक सामान्य ऐप्लिकेशन यह है कि Google Workspace होस्ट ऐप्लिकेशन से तीसरे पक्ष के सिस्टम से इंटरैक्ट करने के लिए इंटरफ़ेस उपलब्ध कराया जाए. Apps Script के लिए OAuth2 लाइब्रेरी से, आपको तीसरे पक्ष की सेवाओं के लिए कनेक्शन बनाने और उन्हें मैनेज करने में मदद मिल सकती है.

तीसरे पक्ष के सिस्टम में अक्सर यह ज़रूरी होता है कि उपयोगकर्ता, यूज़र आईडी, पासवर्ड या दूसरे क्रेडेंशियल का इस्तेमाल करके साइन इन करें. जब कोई उपयोगकर्ता Google Workspace के किसी एक होस्ट का इस्तेमाल कर रहा हो और वह तीसरे पक्ष की आपकी सेवा में साइन इन करे, तब आपको यह पक्का करना होगा कि उसे किसी दूसरे Google Workspace होस्ट पर स्विच करते समय फिर से साइन इन न करना पड़े. बार-बार लॉगिन के अनुरोध से बचने के लिए, उपयोगकर्ता प्रॉपर्टी या आईडी टोकन का इस्तेमाल करें. इनके बारे में नीचे दिए गए सेक्शन में बताया गया है.

उपयोगकर्ता प्रॉपर्टी

आपके पास Apps Script की उपयोगकर्ता प्रॉपर्टी में, किसी उपयोगकर्ता के साइन इन का डेटा सेव करने का विकल्प है. जैसे, उनकी लॉगिन सेवा से अपना JWT बनाया जा सकता है और उसे उपयोगकर्ता प्रॉपर्टी में रिकॉर्ड किया जा सकता है. इसके अलावा, उनकी सेवा के लिए उपयोगकर्ता नाम और पासवर्ड भी रिकॉर्ड किया जा सकता है.

उपयोगकर्ता प्रॉपर्टी का दायरा इस तरह होता है कि उन्हें ऐड-ऑन स्क्रिप्ट में वही उपयोगकर्ता ऐक्सेस कर सकता है. दूसरे उपयोगकर्ता और अन्य स्क्रिप्ट, इन प्रॉपर्टी को ऐक्सेस नहीं कर सकतीं. ज़्यादा जानकारी के लिए, PropertiesService पर जाएं.

आईडी टोकन

आपके पास Google आईडी टोकन का इस्तेमाल, अपनी सेवा के लॉगिन क्रेडेंशियल के तौर पर करने का विकल्प है. यह सिंगल साइन-ऑन हासिल करने का एक तरीका है. उपयोगकर्ताओं ने पहले ही Google में लॉग इन किया हुआ है, क्योंकि वे किसी Google होस्ट ऐप्लिकेशन में हैं.