Node.js का क्विकस्टार्ट

क्विकस्टार्ट में, Google Workspace API को कॉल करने वाले ऐप्लिकेशन को सेट अप और चलाने का तरीका बताया गया है. इस क्विकस्टार्ट में, पुष्टि करने का आसान तरीका इस्तेमाल किया गया है. यह टेस्टिंग एनवायरमेंट के लिए सही है. हमारा सुझाव है कि प्रोडक्शन एनवायरमेंट के लिए, ऐक्सेस क्रेडेंशियल चुनने से पहले, पुष्टि करने और अनुमति देने के बारे में जान लें. इससे आपको अपने ऐप्लिकेशन के लिए सही क्रेडेंशियल चुनने में मदद मिलेगी.

Drive Labels API को अनुरोध भेजने वाला Node.js कमांड-लाइन ऐप्लिकेशन बनाएं.

मकसद

  • अपना एनवायरमेंट सेट अप करें.
  • क्लाइंट लाइब्रेरी इंस्टॉल करें.
  • सैंपल सेट अप करें.
  • सैंपल चलाएं.

ज़रूरी शर्तें

  • एक Google खाता.

अपना एनवायरमेंट सेट अप करने का तरीका

इस क्विकस्टार्ट को पूरा करने के लिए, अपना एनवायरमेंट सेट अप करें.

एपीआई चालू करना

Google API का इस्तेमाल करने से पहले, आपको उन्हें Google Cloud प्रोजेक्ट में चालू करना होगा. एक ही Google Cloud प्रोजेक्ट में, एक या उससे ज़्यादा एपीआई चालू किए जा सकते हैं.

डेस्कटॉप ऐप्लिकेशन के लिए क्रेडेंशियल को अनुमति देना

असली उपयोगकर्ताओं की पुष्टि करने और अपने ऐप्लिकेशन में उपयोगकर्ता का डेटा ऐक्सेस करने के लिए, आपको एक या उससे ज़्यादा OAuth 2.0 क्लाइंट आईडी बनाने होंगे. क्लाइंट आईडी का इस्तेमाल, Google के OAuth सर्वर पर किसी एक ऐप्लिकेशन की पहचान करने के लिए किया जाता है. अगर आपका ऐप्लिकेशन कई प्लैटफ़ॉर्म पर चलता है, तो आपको हर प्लैटफ़ॉर्म के लिए अलग क्लाइंट आईडी बनाना होगा.
  1. Google Cloud console में, मेन्यू > Google Auth platform > क्लाइंट पर जाएं.

    क्लाइंट पर जाएं

  2. क्लाइंट बनाएं पर क्लिक करें.
  3. ऐप्लिकेशन का टाइप > डेस्कटॉप ऐप्लिकेशन पर क्लिक करें.
  4. नाम फ़ील्ड में, क्रेडेंशियल के लिए कोई नाम टाइप करें. यह नाम सिर्फ़ Google Cloud Console में दिखता है.
  5. बनाएं पर क्लिक करें.

    नया क्रेडेंशियल, "OAuth 2.0 क्लाइंट आईडी" में दिखता है.

  6. डाउनलोड की गई JSON फ़ाइल को credentials.json के तौर पर सेव करें. इसके बाद, फ़ाइल को अपनी वर्किंग डायरेक्ट्री में ले जाएं.

क्लाइंट लाइब्रेरी इंस्टॉल करना

  • npm का इस्तेमाल करके लाइब्रेरी इंस्टॉल करें:

    npm install googleapis@113 @google-cloud/local-auth@2.1.1 --save
    

सैंपल सेट अप करना

  1. अपनी वर्किंग डायरेक्ट्री में, index.js नाम की एक फ़ाइल बनाएं.

  2. फ़ाइल में, यह कोड चिपकाएं:

        const fs = require('fs');
        const readline = require('readline');
        const {google} = require('googleapis');
    
        // If modifying these scopes, delete token.json.
        const SCOPES = ['https://www.googleapis.com/auth/drive.labels.readonly'];
        // The file token.json stores the user's access and refresh tokens, and is
        // created automatically when the authorization flow completes for the first
        // time.
        const TOKEN_PATH = 'token.json';
    
        // Load client secrets from a local file.
        fs.readFile('credentials.json', (err, content) => {
          if (err) return console.log('Error loading client secret file:', err);
          // Authorize a client with credentials, then call the Google Drive Labels
          // API.
          authorize(JSON.parse(content), listDriveLabels);
        });
    
        /**
        * Create an OAuth2 client with the given credentials, and then execute the
        * given callback function.
        * @param {Object} credentials The authorization client credentials.
        * @param {function} callback The callback to call with the authorized client.
        */
        function authorize(credentials, callback) {
          const {client_secret, client_id, redirect_uris} = credentials.installed;
          const oAuth2Client = new google.auth.OAuth2(
              client_id, client_secret, redirect_uris[0]);
    
          // Check if we have previously stored a token.
          fs.readFile(TOKEN_PATH, (err, token) => {
            if (err) return getNewToken(oAuth2Client, callback);
            oAuth2Client.setCredentials(JSON.parse(token));
            callback(oAuth2Client);
          });
        }
    
        /**
        * Get and store new token after prompting for user authorization, and then
        * execute the given callback with the authorized OAuth2 client.
        * @param {google.auth.OAuth2} oAuth2Client The OAuth2 client to get token for.
        * @param {getEventsCallback} callback The callback for the authorized client.
        */
        function getNewToken(oAuth2Client, callback) {
          const authUrl = oAuth2Client.generateAuthUrl({
            access_type: 'offline',
            scope: SCOPES,
          });
          console.log('Authorize this app by visiting this url:', authUrl);
          const rl = readline.createInterface({
            input: process.stdin,
            output: process.stdout,
          });
          rl.question('Enter the code from that page here: ', (code) => {
            rl.close();
            oAuth2Client.getToken(code, (err, token) => {
              if (err) return console.error('Error retrieving access token', err);
              oAuth2Client.setCredentials(token);
              // Store the token to disk for later program executions
              fs.writeFile(TOKEN_PATH, JSON.stringify(token), (err) => {
                if (err) return console.error(err);
                console.log('Token stored to', TOKEN_PATH);
              });
              callback(oAuth2Client);
            });
          });
        }
    
        function listDriveLabels(auth) {
          const service = google.drivelabels({version: 'v2', auth});
          const params = {
            'view': 'LABEL_VIEW_FULL'
          };
          service.labels.list(params, (err, res) => {
            if (err) return console.error('The API returned an error: ' + err);
            const labels = res.data.labels;
            if (labels) {
              labels.forEach((label) => {
                const name = label.name;
                const title = label.properties.title;
                console.log(`${name}\t${title}`);
              });
            } else {
              console.log('No Labels');
            }
          });
        }
    

सैंपल चलाना

  1. अपनी वर्किंग डायरेक्ट्री में, सैंपल चलाएं:

    node .
    
  2. पहली बार सैंपल चलाने पर, आपको ऐक्सेस की अनुमति देने के लिए कहा जाएगा:

    1. अगर आपने Google खाते में पहले से साइन इन नहीं किया है, तो आपको साइन इन करने के लिए कहा जाएगा. अगर आपने एक से ज़्यादा खातों में साइन इन किया हुआ है, तो पुष्टि के लिए किसी एक खाते को चुनें.
    2. स्वीकार करें पर क्लिक करें.

    अनुमति से जुड़ी जानकारी फ़ाइल सिस्टम में सेव होती है. इसलिए, अगली बार सैंपल कोड चलाने पर, आपको अनुमति देने के लिए नहीं कहा जाएगा.

आपने Drive Labels API को अनुरोध भेजने वाला पहला Nodejs ऐप्लिकेशन बना लिया है.

अगले चरण