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

क्विकस्टार्ट उस ऐप्लिकेशन को सेट अप करने और चलाने का तरीका बताता है जो Google Workspace API को कॉल करता है.

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

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

मकसद

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

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

  • आपके पास एक Google खाता होना चाहिए.

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

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

एपीआई चालू करें

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

डेस्कटॉप ऐप्लिकेशन के लिए क्रेडेंशियल की पुष्टि करना

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

    क्रेडेंशियल पर जाएं

  2. क्रेडेंशियल बनाएं > OAuth क्लाइंट आईडी पर क्लिक करें.
  3. ऐप्लिकेशन प्रकार > डेस्कटॉप ऐप्लिकेशन पर क्लिक करें.
  4. नाम फ़ील्ड में, क्रेडेंशियल के लिए नाम लिखें. यह नाम सिर्फ़ Google Cloud Console में दिखता है.
  5. बनाएं पर क्लिक करें. अब OAuth क्लाइंट की स्क्रीन पर आपका नया क्लाइंट आईडी और क्लाइंट सीक्रेट दिखेगा.
  6. ठीक है पर क्लिक करें. नया क्रेडेंशियल, OAuth 2.0 Client ID में दिखता है.
  7. डाउनलोड की गई JSON फ़ाइल को credentials.json के तौर पर सेव करें और उसे अपनी वर्क डायरेक्ट्री में ले जाएं.

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

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

    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. स्वीकार करें पर क्लिक करें.

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

आपने अपना पहला Nodejs ऐप्लिकेशन बना लिया है, जिससे Drive Labels API के लिए अनुरोध किए जाते हैं.

अगले चरण