valeriangalliat / spotify-buddylist

Fetch the friend activity Spotify feed.
122 stars 10 forks source link

Spotify API Wrapper not working #4

Open ironjaw72 opened 11 months ago

ironjaw72 commented 11 months ago

I get a 403 error (unauthorized) when trying to use the wrapper (wrapWebApi) feature. I put the exact example code with my spDcCookie info and it doesn't work. The same cookie and code works fine when not using the wrapper.

const SpotifyWebApi = require('spotify-web-api-node') const buddyList = require('spotify-buddylist')

const api = buddyList.wrapWebApi(new SpotifyWebApi({ spDcCookie }))

const tokenResponse = await api.getWebAccessToken() api.setAccessToken(tokenResponse.body.accessToken)

const friendActivityResponse = await api.getFriendActivity() const friendActivity = friendActivityResponse.body

Error printout is: body: {}, headers: { date: 'Thu, 17 Aug 2023 19:51:18 GMT', 'content-type': 'text/html; charset=utf-8', vary: 'Accept-Encoding', 'content-encoding': 'gzip', 'x-envoy-upstream-service-time': '0', 'sp-trace-id': 'bb8a47291bedb1f1', 'strict-transport-security': 'max-age=31536000', 'x-content-type-options': 'nosniff', server: 'envoy', via: 'HTTP/2 edgeproxy, 1.1 google', 'alt-svc': 'h3=":443"; ma=2592000,h3-29=":443"; ma=2592000', connection: 'close', 'transfer-encoding': 'chunked' }, statusCode: 403

ironjaw72 commented 10 months ago

I added this to index.js to accomplish what I wanted rather than using the wrapWebApi. It works well.

exports.getMyCurrentPlaybackState = async function getMyCurrentPlaybackState (webAccessToken) {

  const res = await fetch('https://api.spotify.com/v1/me/player', {
    headers: {
      Authorization: `Bearer ${webAccessToken}`
    }
  })

  if (res.status != 200) { return res.status }  

  return res.json()
}

exports.getMyCurrentPlayingTrack = async function getMyCurrentPlayingTrack (webAccessToken) {
  const res = await fetch('https://api.spotify.com/v1/me/player/currently-playing', {
    headers: {
      Authorization: `Bearer ${webAccessToken}`
    }
  })

  return res.json()
}