物理の駅 Physics station by 現役研究者

テクノロジーは共有されてこそ栄える

Google Apps Script(GAS): SpotifyのAPIでプレイリストを取得する

Google Apps Script経由で、SpotifyのAPIを使って、プレイリストを取得してみよう

function myFunction() {
  var clientId = "set your clientId here";
  var clientSecret = "set your client secret here";

  var url = "https://accounts.spotify.com/api/token";
  var params = {
    method: "post",
    headers: { "Authorization": "Basic " + Utilities.base64Encode(clientId + ":" + clientSecret) },
    payload: { grant_type: "client_credentials" },
  };
  var response = UrlFetchApp.fetch(url, params);
  var access_token = JSON.parse(response.getContentText()).access_token;

  var playlists = ["4tY0lHoV8IemMBp4iTnKnl", "799Fo9to2TK0ahGEJsvcm2"];
  for (var i = 0; i < playlists.length; i++) {
    var url = "https://api.spotify.com/v1/playlists/" + playlists[i];
    var params = {
      method: 'get',
      headers: { 'Authorization': 'Bearer ' + access_token }
    };

    var response = UrlFetchApp.fetch(url, params);
    var data = JSON.parse(response.getContentText());
    Logger.log(data.name + " " + data.tracks.items.length)
  }
}