如何使用多个随机 YouTube v3 API 密钥

How can I use multiple random YouTube v3 API keys

我在下面有这段代码,我想使用从密钥列表中随机选择的 API 密钥:

function search() {
  // Clear Results
  $('#results').html('');
  $('#buttons').html('');

  // Get Form Input
  q = $('#query').val();

  // Run Get Request on API
  $.get(
    "https://www.googleapis.com/youtube/v3/search", {
      part: 'snippet, id',
      q: q,
      maxResults: 10,
      type: 'video',
      key: 'eeHFSFqK7zw9IrUyNCCuoAIzaSyCGEIc9V4qPKl'
    },
    function(data) {
      var nextPageToken = data.nextPageToken;
      var prevPageToken = data.prevPageToken;
      // Log Data
      console.log(data);
      $.each(data.items, function(i, item) {
        // Get Output
        var output = getOutput(item);
        // Display Results
        $('#results').append(output);
      });
      var buttons = getButtons(prevPageToken, nextPageToken);
      // Display Buttons
      $('#buttons').append(buttons);
    }
  );
}

我想更改此代码:

键:'eeHFSFqK7zw9IrUyNCCuoAIzaSyCGEIc9V4qPKl'},

至其他随机API键:

AIzaSyDIPhJMU8UnT5Ge8rp3OJNsoTvCRVNjngd

AIzaS_nb6cvB8r2MR5ATxOZ4W4yBVRxfGc4xpFm

AIzaSzEmOA7qNfcUxDOdNLNzInuwyCmOcuD-OjB

AIzasMirEprAzJ_egdpumvglEcSyDtyL_PT5PCA

AIzaSP1bObfQTaTel2cADI1UyAdCWLOhkjOAEDS

我试过了:

 var r_text = new Array();
 r_text[0] = "AIzaSyDIPhJMU8UnT5Ge8rp3OJNsoTvCRVNjngd";
 r_text[1] = "AIzaS_nb6cvB8r2MR5ATxOZ4W4yBVRxfGc4xpFm";
 r_text[2] = "AIzaSzEmOA7qNfcUxDOdNLNzInuwyCmOcuD-OjB";
 r_text[3] = "AIzasMirEprAzJ_egdpumvglEcSyDtyL_PT5PCA";
 r_text[4] = "AIzaSP1bObfQTaTel2cADI1UyAdCWLOhkjOAEDS";
 var nn = Math.floor(5 * Math.random())



     function search() {
   // Clear Results
   $('#results').html('');
   $('#buttons').html('');

   // Get Form Input
   q = $('#query').val();

   // Run Get Request on API
   $.get(
     "https://www.googleapis.com/youtube/v3/search", {
       part: 'snippet, id',
       q: q,
       maxResults: 10,
       type: 'video',
       key: r_text[nn]
     },
     function(data) {
       var nextPageToken = data.nextPageToken;
       var prevPageToken = data.prevPageToken;
       // Log Data
       console.log(data);
       $.each(data.items, function(i, item) {
         // Get Output
         var output = getOutput(item);
         // Display Results
         $('#results').append(output);
       });
       var buttons = getButtons(prevPageToken, nextPageToken);
       // Display Buttons
       $('#buttons').append(buttons);
     }
   );
 }

但是不工作:/

您应该尝试将随机数变量放在搜索函数中,这样每次调用 search() 函数时,都会选择随机 API 键:

 var r_text = new Array();
 r_text[0] = "AIzaSyDIPhJMU8UnT5Ge8rp3OJNsoTvCRVNjngd";
 r_text[1] = "AIzaS_nb6cvB8r2MR5ATxOZ4W4yBVRxfGc4xpFm";
 r_text[2] = "AIzaSzEmOA7qNfcUxDOdNLNzInuwyCmOcuD-OjB";
 r_text[3] = "AIzasMirEprAzJ_egdpumvglEcSyDtyL_PT5PCA";
 r_text[4] = "AIzaSP1bObfQTaTel2cADI1UyAdCWLOhkjOAEDS";


function search() {
    var nn = Math.floor(5 * Math.random());
   // Clear Results
   $('#results').html('');
   $('#buttons').html('');

   // Get Form Input
   q = $('#query').val();

   // Run Get Request on API
   $.get(
     "https://www.googleapis.com/youtube/v3/search", {
       part: 'snippet, id',
       q: q,
       maxResults: 10,
       type: 'video',
       key: r_text[nn]
     },
     function(data) {
       var nextPageToken = data.nextPageToken;
       var prevPageToken = data.prevPageToken;
       // Log Data
       console.log(data);
       $.each(data.items, function(i, item) {
         // Get Output
         var output = getOutput(item);
         // Display Results
         $('#results').append(output);
       });
       var buttons = getButtons(prevPageToken, nextPageToken);
       // Display Buttons
       $('#buttons').append(buttons);
     }
   );
 }