简体   繁体   中英

405 Error when using Google Apps Script to make Post/Put calls using Spotify API

I am trying to do a PUT and DELETE request to the Spotify API using Google Apps Script. However when I do this I get the error: '405 Not Allowed'.

I am really puzzled as to why this is because I am able to make a GET request ok to the same resource, and I am also able to do these PUT and DELETE requests fine in programs like Paw and Postman, but when I do it in Google Apps Script I get the 405 error. This is my current code:

var authorisation = "Bearer "+accessToken;

var getUrl = baseURL+'v1/users/'+user+'/playlists/'+playlist+'/tracks';

var replaceOptions = {
      "method":"put",
      "contentType":"application/json",
      "payload":JSON.stringify({"uris": ["spotify:track:4UaGt8uW0srvzFZAwKCumD","spotify:track:5QmvryaxUQyXyFMljdgOPD","spotify:track:5NFNxnghrwogF3H6Pj66f6"]}),
      "headers":{"Authorization":authorisation},
      'muteHttpExceptions':true
    };

  var replaceResponse = UrlFetchApp.fetch(getUrl, replaceOptions);

  Logger.log(replaceResponse.getAllHeaders());
  Logger.log(replaceResponse.getContentText());
  Logger.log(replaceResponse.getResponseCode());

I would really appreciate some insight as to what is going on. FYI this is the original endpoint documentation here .

Sorry, this ended up being a stupid error. I somehow ended up using the base URL from the authentication endpoint of Spotify and not the API endpoint.

I should have posted the full code in my question!

If anyone else runs into this silly issue by mistake, the base URL of authentication is: https://accounts.spotify.com

whereas the api base url is: https://api.spotify.com

Point of learning; just because a lot of API's have one standard URL endpoint not all of them do! And, never overlook the simple things.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM