I have the following code snippet and I am looking to randomly select an API key from a list of keys:
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);
}
);
}
I would like to change this line of code:
key: 'eeHFSFqK7zw9IrUyNCCuoAIzaSyCGEIc9V4qPKl'},
To use other random API keys listed below:
AIzaSyDIPhJMU8UnT5Ge8rp3OJNsoTvCRVNjngd
AIzaS_nb6cvB8r2MR5ATxOZ4W4yBVRxfGc4xpFm
AIzaSzEmOA7qNfcUxDOdNLNzInuwyCmOcuD-OjB
AIzasMirEprAzJ_egdpumvglEcSyDtyL_PT5PCA
AIzaSP1bObfQTaTel2cADI1UyAdCWLOhkjOAEDS
I attempted the following approach:
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);
}
);
}
However, it is not functioning as expected :/