Not sure how to syntax the header bearer token portion of this..
...
// --------------------------------------------------------------------------------------------------
//
// iTunes Music Discovery Application in Google Sheets
//
// --------------------------------------------------------------------------------------------------
// custom menu
function onOpen() {
var ui = SpreadsheetApp.getUi();
ui.createMenu('iTunes API Call')
.addItem('Get Artist Data','displayArtistData')
.addToUi();
}
// function to call iTunes API
function calliTunesAPI(artist) {
// Call the iTunes API
var response = UrlFetchApp.fetch("https://api.music.apple.com/v1/catalog/us/music-videos/{" + artist + "}");
--header 'Content-Type:application/json' \
--header 'Authorization: Bearer eyJhbGciOiJFUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6IjVWNjY3SEZaNjMifQ.eyJpc3MiOiJTOTU0QVE5Q1Q3IieOwO'
// Parse the JSON reply
var json = response.getContentText();
return JSON.parse(json);
}
function displayArtistData() {
// pick up the search term from the Google Sheet
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var artist = sheet.getRange(11,3).getValue();
var tracks = calliTunesAPI(artist);
var results = tracks["results"];
var output = []
results.forEach(function(elem,i) {
var image = '=image("' + elem["artworkUrl60"] + '",4,60,60)';
var hyperlink = '=hyperlink("' + elem["previewUrl"] + '","Listen to preview")';
output.push([elem["artistName"],elem["collectionName"],elem["trackName"],image,hyperlink]);
sheet.setRowHeight(i+15,65);
});
// sort by album
var sortedOutput = output.sort( function(a,b) {
var albumA = (a[1]) ? a[1] : 'Not known'; // in case album name undefined
var albumB = (b[1]) ? b[1] : 'Not known'; // in case album name undefined
if (albumA < albumB) { return -1; } else if (albumA > albumB) {
return 1;
}
// names are equal
return 0;
});
// adds an index number to the array
sortedOutput.forEach(function(elem,i) {
elem.unshift(i + 1);
});
var len = sortedOutput.length;
// clear any previous content
sheet.getRange(15,1,500,6).clearContent();
// paste in the values
sheet.getRange(15,1,len,6).setValues(sortedOutput);
// formatting
sheet.getRange(15,1,500,6).setVerticalAlignment("middle");
sheet.getRange(15,5,500,1).setHorizontalAlignment("center");
sheet.getRange(15,2,len,3).setWrap(true);
}
...
This part of the code isn't working:
--header 'Content-Type:application/json'
--header 'Authorization: Bearer eyJhbGciOiJFUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6IjVWNjY3SEZaNjMifQ.eyJpc3MiOiJTOTU0QVE5Q1Q3IieOwO
Not sure how to syntax this correctly. I'm sure this is 101, but not sure the answer.
With this as example you should be able to update your code. Good luck!
var url = "https://www.example.com"
var headers = {
"Authorization": "Bearer ijfewfkjsfkjh4543",
"Content-Type": "Application/json"
};
var options = {
"method" : "get",
"headers" : headers
};
var response = UrlFetchApp.fetch(url, options);
EDIT: This should do it but the API is down i see. You can delete the var artist. But i needed a number ;)
function calliTunesAPI(artist) {
var artist = 178834;
var url = "api.music.apple.com/v1/catalog/us/music-videos"+artist;
var headers = { "Authorization": "Bearer eyJhbGciOiJFUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6IjVWNjY3SEZaNjMifQ.eyJpc3MiOiJTOTU0QVE5Q1Q3IieOwO",
"Content-Type": "Application/json"
};
var options = {
"method": "get",
"headers": headers,
"muteHttpExceptions": true
};
var response = UrlFetchApp.fetch(url, options).getContentText();
var json = JSON.parse(response);
console.log(json);
}
You might want to take a look at the UrlFetchApp
documentation here .
What UrlFetchApp
does is to fetch resources and communicate with other hosts over the internet. Since you want to retrieve some data, you will essentially have to include the bearer
and the headers
along when making the request.
With all these taking into account, you can rewrite your function to this:
function calliTunesAPI(artist) {
var url = "https://api.music.apple.com/v1/catalog/us/music-videos/{" + artist + "}";
var params = {
"headers": {
"Content-Type": "application/json",
"Authorization": "Bearer eyJhbGciOiJFUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6IjVWNjY3SEZaNjMifQ.eyJpc3MiOiJTOTU0QVE5Q1Q3IieOwO"
}
"method": "GET"
}
var response = UrlFetchApp(url, params);
var json = response.getContentText();
return JSON.parse(json);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.