How can I use multiple random YouTube v3 API keys in javascript

You should try to put the random number variable within the search function, so each time the search() function is being called, random API key is being picked:

 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);
     }
   );
 }

Through PHP

$apikey=array(“AIzaSyAzWRwUZZDyy1QmwlmSKzxGCELJ2dYnDEMU”,”AIzaSyBGXPpdCC_sivaZAZ1I7Zq_weIAgtQuo2kc”,”AIzaSyBINY2ziseK60nN23-JU-CWlMmLhoGeN_U”,”AIzaSyAVY51pLSPxU36IA1yvy4tqnCnKznzT6LCE”,”AIzaSyAz1fcvnkMbN2CfZIF_zlnbg8R20rX4V1mM”,”AIzaSyBQBUd3N6l-cbiP6NhQpZTT5Hzl1XadgaVA”,”AIzaSyBXR9w_Xj1549fkG4dWfJgPl2ag5tHlO-QU”,”AIzaSyC9fpBSnlyRs_jGIKarNO_ZkF4nWtLza1I”,”AIzaSyDP3cG_uopwEx4c_aSbxPficMPoYCqlMb8c”,”AIzaSyBdjg9oobvF9WKRKQVgk2BSCrmHOmPeWGUg”,”AIzaSyAzUvDNY1UaG3TvuYLMMJKJctTt5i1DIKOY”,”AIzaSyAez84wR3m98sxPV0awy1OtZ6Uupztoa58g”,”AIzaSyC1lkVvOl2VJiZ5A8yqtNPkieZtOnawRzk”,”AIzaSyD8UMVBIi2CjD_GBBuw3ObIPp01Jc4QkYVQ”,”AIzaSyBqr1kU4VljyFKQCIm6PRdbbFjEc8YkrYL4″,”AIzaSyDvTtU9zkxYmWYXqmCA4fvZmpFKgZzv00CtU”,”AIzaSyDvRddOkZ-mwPgL8TWEGXskFRxYa4AiMZW0″,”AIzaSyDF4ihs5IBlGwO-0ylq-R89DY_H23FHN7-g”,”AIzaSyBoVmJYIq4BkprkBLuRm16AJqebSFzhOLM”,”AIzaSyBKTLVcntTm8SkaIzNrGZgqZXaWWfdrsoY”,”AIzaSyDRmayGRAcKl3wXRndr83LYFMpMN-MQJ8xA”,”AIzaSyBLWwqDfcs-EZa6Uxs7kdxpf2PzSoMCNuxs”,”AIzaSyABgEOnj4LR-QVnqFDnHsWia8YWcT8weVMBo”,”AIzaSyBCsLYaJpxDY3Uota_RWazn4KgetUG6tfKQk”,”AIzaSyAo0CxyCKteqS8KEl_4OW6PJPGweS9PDTK4″);


$random_key = $apikey[array_rand($apikey)];

$gdata = file_get_contents(‘https://www.googleapis.com/youtube/v3/search?part=snippet&q=’ . urlencode(str_replace(“-“, “+”, $q)) . ‘&maxResults=’.$limit.’&key=’ . $random_key);

Leave a Comment

Your email address will not be published. Required fields are marked *

Open chat