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

बाकी के पेज में बताए गए चरणों को पूरा करें और करीब पांच मिनट में, आपके पास एक आसान Node.js कमांड लाइन ऐप्लिकेशन होगा जो YouTube Data API के लिए अनुरोध करेगा.

इस गाइड में इस्तेमाल किया गया सैंपल कोड, channelGoogle Developers डेवलपर के YouTube चैनल के लिए उपलब्ध रिसॉर्स को इकट्ठा करता है और उस रिसॉर्स से जुड़ी कुछ बुनियादी जानकारी को प्रिंट करता है.

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

इस क्विकस्टार्ट को चलाने के लिए, आपको इनकी ज़रूरत होगी:

  • Node.js इंस्टॉल किया गया.
  • npm पैकेज प्रबंधन टूल (Node.js के साथ आता है).
  • इंटरनेट और वेब ब्राउज़र का ऐक्सेस.
  • एक Google खाता.

पहला चरण: YouTube Data API चालू करना

  1. Google Developers Console में कोई प्रोजेक्ट बनाने या चुनने के लिए, इस विज़ार्ड का इस्तेमाल करें और एपीआई को अपने-आप चालू करें. जारी रखें पर क्लिक करें. इसके बाद, क्रेडेंशियल पर जाएं पर क्लिक करें.

  2. क्रेडेंशियल बनाएं पेज पर, रद्द करें बटन पर क्लिक करें.

  3. पेज पर सबसे ऊपर, OAuth की सहमति वाली स्क्रीन टैब को चुनें. ईमेल पता चुनें, अगर प्रॉडक्ट का नाम पहले से सेट नहीं है, तो उसे डालें. इसके बाद, सेव करें बटन पर क्लिक करें.

  4. क्रेडेंशियल टैब चुनें, क्रेडेंशियल बनाएं बटन पर क्लिक करें और OAuth क्लाइंट आईडी चुनें.

  5. ऐप्लिकेशन टाइप अन्य चुनें, नाम "YouTube Data API क्विकस्टार्ट" डालें, और बनाएं बटन पर क्लिक करें.

  6. मिलने वाले डायलॉग को खारिज करने के लिए, ठीक है पर क्लिक करें.

  7. क्लाइंट आईडी के दाईं ओर मौजूद, (JSON डाउनलोड करें) बटन पर क्लिक करें.

  8. डाउनलोड की गई फ़ाइल को अपनी वर्किंग डायरेक्ट्री में ले जाएं और उसका नाम बदलें client_secret.json.

दूसरा चरण: क्लाइंट लाइब्रेरी इंस्टॉल करना

npm का उपयोग करके लाइब्रेरी इंस्टॉल करने के लिए निम्न आदेश चलाएं:

npm install googleapis --save
npm install google-auth-library --save

तीसरा चरण: सैंपल सेट अप करना

अपनी वर्किंग डायरेक्ट्री में quickstart.js नाम की फ़ाइल बनाएं और नीचे दिए गए कोड में कॉपी करें:

var fs = require('fs');
var readline = require('readline');
var {google} = require('googleapis');
var OAuth2 = google.auth.OAuth2;

// If modifying these scopes, delete your previously saved credentials
// at ~/.credentials/youtube-nodejs-quickstart.json
var SCOPES = ['https://www.googleapis.com/auth/youtube.readonly'];
var TOKEN_DIR = (process.env.HOME || process.env.HOMEPATH ||
    process.env.USERPROFILE) + '/.credentials/';
var TOKEN_PATH = TOKEN_DIR + 'youtube-nodejs-quickstart.json';

// Load client secrets from a local file.
fs.readFile('client_secret.json', function processClientSecrets(err, content) {
  if (err) {
    console.log('Error loading client secret file: ' + err);
    return;
  }
  // Authorize a client with the loaded credentials, then call the YouTube API.
  authorize(JSON.parse(content), getChannel);
});

/**
 * 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) {
  var clientSecret = credentials.installed.client_secret;
  var clientId = credentials.installed.client_id;
  var redirectUrl = credentials.installed.redirect_uris[0];
  var oauth2Client = new OAuth2(clientId, clientSecret, redirectUrl);

  // Check if we have previously stored a token.
  fs.readFile(TOKEN_PATH, function(err, token) {
    if (err) {
      getNewToken(oauth2Client, callback);
    } else {
      oauth2Client.credentials = 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 to call with the authorized
 *     client.
 */
function getNewToken(oauth2Client, callback) {
  var authUrl = oauth2Client.generateAuthUrl({
    access_type: 'offline',
    scope: SCOPES
  });
  console.log('Authorize this app by visiting this url: ', authUrl);
  var rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
  });
  rl.question('Enter the code from that page here: ', function(code) {
    rl.close();
    oauth2Client.getToken(code, function(err, token) {
      if (err) {
        console.log('Error while trying to retrieve access token', err);
        return;
      }
      oauth2Client.credentials = token;
      storeToken(token);
      callback(oauth2Client);
    });
  });
}

/**
 * Store token to disk be used in later program executions.
 *
 * @param {Object} token The token to store to disk.
 */
function storeToken(token) {
  try {
    fs.mkdirSync(TOKEN_DIR);
  } catch (err) {
    if (err.code != 'EEXIST') {
      throw err;
    }
  }
  fs.writeFile(TOKEN_PATH, JSON.stringify(token), (err) => {
    if (err) throw err;
    console.log('Token stored to ' + TOKEN_PATH);
  });
}

/**
 * Lists the names and IDs of up to 10 files.
 *
 * @param {google.auth.OAuth2} auth An authorized OAuth2 client.
 */
function getChannel(auth) {
  var service = google.youtube('v3');
  service.channels.list({
    auth: auth,
    part: 'snippet,contentDetails,statistics',
    forUsername: 'GoogleDevelopers'
  }, function(err, response) {
    if (err) {
      console.log('The API returned an error: ' + err);
      return;
    }
    var channels = response.data.items;
    if (channels.length == 0) {
      console.log('No channel found.');
    } else {
      console.log('This channel\'s ID is %s. Its title is \'%s\', and ' +
                  'it has %s views.',
                  channels[0].id,
                  channels[0].snippet.title,
                  channels[0].statistics.viewCount);
    }
  });
}

चौथा चरण: सैंपल चलाना

नीचे दिए गए निर्देश का इस्तेमाल करके, नमूने को चलाएं:

node quickstart.js

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

  1. अपने वेब ब्राउज़र में दिए गए यूआरएल को ब्राउज़ करें.

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

  2. स्वीकार करें बटन पर क्लिक करें.
  3. आपको दिया गया कोड कॉपी करें, उसे कमांड-लाइन प्रॉम्प्ट में चिपकाएं और Enter दबाएं.

नोट

  • अनुमति से जुड़ी जानकारी को फ़ाइल सिस्टम में सेव किया जाता है, इसलिए आगे दी गई प्रोसेस करने की अनुमति नहीं दी जाएगी.
  • इस उदाहरण में ऑथराइज़ेशन फ़्लो को कमांड लाइन ऐप्लिकेशन के लिए बनाया गया है. 'YouTube डेटा एपीआई' का इस्तेमाल करने वाले वेब ऐप्लिकेशन में अनुमति देने का तरीका जानने के लिए, वेब सर्वर ऐप्लिकेशन के लिए OAuth 2.0 का इस्तेमाल करना देखें.

    अन्य संदर्भों में प्राधिकरण करने के तरीके के बारे में जानकारी के लिए, लाइब्रेरी के README का अनुमति देना और प्रमाणीकरण करना देखें.

इसके बारे में और पढ़ें