spotify / web-api

This issue tracker is no longer used. Join us in the Spotify for Developers forum for support with the Spotify Web API ➡️ https://community.spotify.com/t5/Spotify-for-Developers/bd-p/Spotify_Developer
981 stars 79 forks source link

How do I use API in Javascript #1427

Open KonkenBonken opened 4 years ago

KonkenBonken commented 4 years ago

On this Spotify gives a code to get the User's Currently Playing Track using CURL. Like this: curl -X "GET" "https://api.spotify.com/v1/me/player/currently-playing" -H "Accept: application/json" -H "Content-Type: application/json" -H "Authorization: Bearer BQBSzHFnjIuzHSAGKWlOc9999999999mVD1GfHM6v7amACnUw1PTJg8njubEzMWEKNiPe06H_gc5eu3wWEynsF1h4q2UY6YGRFGSXagdvpxhDV_kBA6qTK9999999999u9ulPsbkTW5mgRTy7T3dgBEVNjaMZC1KQPLT1Txbc20999tbXe38OW73YCNe4aT4uO3aMg"

But how do I get the User's Currently Playing Track using Javascript?

OBS. I've changed the token in this post

0xNF commented 4 years ago

Issue a fetch request.

For example:

const myHeaders = new Headers({
  'Content-Type': 'application/json',
  'Accept': 'application/json',
  'Authorization': 'Bearer BQBSzHFnjIuzHSAGKWlOc9999999999mVD1GfHM6v7amACnUw1PTJg8njubEzMWEKNiPe06H_gc5eu3wWEynsF1h4q2UY6YGRFGSXagdvpxhDV_kBA6qTK9999999999u9ulPsbkTW5mgRTy7T3dgBEVNjaMZC1KQPLT1Txbc20999tbXe38OW73YCNe4aT4uO3aMg'
});

const myRequest = new Request('https://api.spotify.com/v1/me/player/currently-playing', {
  method: 'GET',
  headers: myHeaders,
});

fetch(myRequest)
  .then((response) => response.body())
  .then((receivedBody) => {
    // your code here
  });