0

We need to run the axios.post equivalent for following cURL command in nodeJS:

  curl -H "Authorization: Basic ZjM4Zj...Y0MzE=" -d grant_type=refresh_token -d refresh_token=NgAagA...NUm_SHo https://accounts.spotify.com/api/token

My Approach:

axios.post("https://accounts.spotify.com/api/token", {
  grant_type: 'refresh_token',
  refresh_token: 'NgAagA...NUm_SHo',
  header: {
    Authorization: 'Basic ZjM4Zj...Y0MzE=',
  }
}).then((resAxios) => {
  console.log(resAxios.data)
  spotifyResult = resAxios.data;
}).catch((error) => {
  console.error(error)
})

Above code returns the following error in reponse:

statusCode: 415,
statusMessage: 'Unsupported Media Type'

Format:

refresh_token should be application/x-www-form-urlencoded

Authorization: Basic <base64 encoded client_id:client_secret>

Refer the 'Authorization Code Flow' for the cURL command, here:

https://developer.spotify.com/documentation/general/guides/authorization-guide/

Feel free to provide with any other variant, i.e., instead of using axios. I preferred it, since it parses the fetched data as well. Please provide with the code to parse it with, if so. I'm a 'beginner' beginner.

Varun
  • 73
  • 1
  • 10
  • What did you try so far? – Manuel Spigolon May 20 '21 at 06:35
  • Edited the question. – Varun May 20 '21 at 06:40
  • I'm not sure, but I think I haven't used the proper syntax. I don't know how to include request body parameters. – Varun May 20 '21 at 06:47
  • try this: https://stackoverflow.com/questions/47630163/axios-post-request-to-send-form-data – Marc Stroebel May 20 '21 at 06:59
  • Or maybe it has something to do with `'content-type'` – Varun May 20 '21 at 07:03
  • I tried this approach. The server didn't start and returned, `ReferenceError: FormData is not defined'. – Varun May 20 '21 at 07:10
  • @vcboi Have you tried sending `content-type` inside `header`? – zx01 May 20 '21 at 07:12
  • @zx01 Yep, I tried putting it next to `Authorization` as `'content-type': 'application/json'` – Varun May 20 '21 at 07:20
  • The `ReferenceError: FormData is not defined` was in response to suggestion given by @MarcStroebel – Varun May 20 '21 at 07:22
  • @vcboi Try using `'Content-Type': 'application/x-www-form-urlencoded'`. – zx01 May 20 '21 at 07:24
  • FormData is a npm package, did you install it? https://www.npmjs.com/package/form-data – Marc Stroebel May 20 '21 at 07:25
  • @zx01 No good. Returned with the same error. On a side-note, why do we put `content-type` in quotation marks, while all the others like `Authorization` are not in quotation marks? – Varun May 20 '21 at 07:27
  • @MarcStroebel Oh. I wasn't aware. Just did. Returned with the same error. It's actually a 100 lined or so long response. Is there anything specific that I should mention in the question, apart from the fact that it returns with status code 415? – Varun May 20 '21 at 07:30
  • 1
    @vcboi Because of the hyphen in between. Hehe. We can put double quotes for other properties as well but its not required. Btw, check this [link](https://stackoverflow.com/questions/48947586/415-coming-back-from-requesting-a-token-spotify-api/48947993). May be this could help. – zx01 May 20 '21 at 07:32
  • another approach, try this sample: https://gist.github.com/akexorcist/ea93ee47d39cf94e77802bc39c46589b – Marc Stroebel May 20 '21 at 07:38
  • @zx01 Alright, got that. Trying the solution on the link. – Varun May 20 '21 at 07:39

1 Answers1

2

Thanks to all the folks: Manuel Spigolon, zx01, Mark Stroebel, Łukasz Szewczak, ponury-kostek, Jakub Luczak, Gerardo Gonzalez :)

I tried this answer on this link, as provided by @zx01 in the comments to the question.

Final code, tailored to the needs:

axios({
    url: "https://accounts.spotify.com/api/token",
    method: "post",
    params: {
        grant_type: "refresh_token",
      //grant_type: "client_credentials", //This works as well.
        refresh_token: 'AQAw95rH0...CnWBE'
    },
    headers: {
      Authorization:'Basic MWQ4Z...dhYmU=',
        "Accept": "application/json",
        "Content-Type": "application/x-www-form-urlencoded"
    },

}).then(function (response) {
    console.log(response.data);
}).catch(function (error) {
    console.log(error);
});

However, the following syntax doesn't seems to work. It again returns with status code 415: Unsupported Media Type.

axios.post("https://accounts.spotify.com/api/token", null, {
    params: {
        grant_type: "refresh_token",
      //grant_type: "client_credentials", //This doesn't work either.
        refresh_token: 'AQAw95rH0...CnWBE'
    },
    headers: {
      Authorization:'Basic MWQ4Z...dhYmU=',
        "Accept": "application/json",
        "Content-Type": "application/x-www-form-urlencoded"
    },
}).then(function (response) {
  console.log(response.data);
}).catch(function (error) {
    console.log(error);
});

It'd be dope if somebody could fix the above code, and reason as to why we're getting that error.

Also notice there is a quomma ',', next to the closing curly braces that packs the 'headers' property.

zx01
  • 541
  • 6
  • 17
Varun
  • 73
  • 1
  • 10
  • 1
    Please try this one because its working for me in postman. Replace `grant_type: "refresh_token"` with `grant_type: "client_credentials"` – zx01 May 20 '21 at 08:31
  • 1
    @zx01 Yes! Thank you so much. It did work. Updating the answer. – Varun May 20 '21 at 08:38
  • @zx01 How did you figure, that `"refresh_token"` can also be replaced? And that it can be replaced to `"client_credentials"`? – Varun May 20 '21 at 08:43
  • `grant_type: client_credentials` doesn't work in the second code sample. – Varun May 20 '21 at 09:07
  • 1
    Ohh sorry, I didn't notice that the two codes are different. I thought both were same. The problem with the second code is that axios.post takes three parameters(url, data and config). Since we are not sending any body/data so we have to pass null as second argument. And the third argument will be the params, header, etc. – zx01 May 20 '21 at 09:59
  • Even in the initial code of yours I failed to notice that you were not passing three parameters inside `axios.post()` – zx01 May 20 '21 at 10:05
  • @zx01 Ohhh. Alright. I wasn't aware, thank you for letting me know. How do we pass null parameter? Simply `null`? – Varun May 21 '21 at 22:44
  • Yes. Simply null as second parameter. – zx01 May 22 '21 at 04:30