When there are no recently played tracks for a user, it returns successfully. When there ARE recently played tracks, I receive a 504.
We first grab the access_token adn refresh_token from our db for the user; create a SpotifyWebApi with client_id, client_secret, access_token, refresh_token; call refreshAccessToken(); set the new tokens in our db for that user; create a NEW SpotifyWebApi with the new access and refresh tokens; then call getMyRecentlyPlayedTracks with the new SpotifyWebAPi.
tokentable
.findAll()
.then(tokens => tokens.forEach(token => {
const currSpotifyApi = new SpotifyWebApi({
clientId: CLIENT_ID,
clientSecret: CLIENT_SECRET,
accessToken: token.access_token,
refreshToken: token.refresh_token,
});
currSpotifyApi.refreshAccessToken().then(data => {
const newSpotifyApi = new SpotifyWebApi({
clientId: CLIENT_ID,
clientSecret: CLIENT_SECRET,
accessToken: data.body.access_token,
refreshToken: data.body.refresh_token,
});
const time = Date.now() - 600e3
newSpotifyApi.getMyRecentlyPlayedTracks({ limit: 5, after: time }).then(data => {
const tracks = data.body.items.map(record => ({
spotifyId: "not retrieved",
played_at: record.played_at,
name: record.track.name,
popularity: record.track.popularity,
duration_ms: record.track.duration_ms,
album: record.track.album.name,
artists: record.track.artists.map(artist => artist.name),
UserId: token.UserId
}));
tracktable.bulkCreate(tracks, { ignoreDuplicates: true }).then(records => console.log(`Stored ${records.length} tracks for ${token.UserId}`));
}).catch(err => {
console.log("failed retrieving recently played tracks")
console.log(err)
})
}, err => console.log(`failed to refresh access token for ${username}: ${err}`));
})).catch(err => {
console.log("failed retrieving data from access token table")
console.log(err)
});
data: app.js:746160 - Stored 0 tracks for 151
data: app.js:746160 - Stored 0 tracks for 152
data: app.js:746160 - Stored 0 tracks for 150
data: app.js:746160 - failed retrieving recently played tracks
data: app.js:746160 - WebapiError: [object Object]
data: app.js:746160 - at _toError (/home/ubuntu/hlf-client-app/node_modules/spotify-web-api-node/src/http-manager.js:47:10)
data: app.js:746160 - at /home/ubuntu/hlf-client-app/node_modules/spotify-web-api-node/src/http-manager.js:71:25
data: app.js:746160 - at Request.callback (/home/ubuntu/hlf-client-app/node_modules/superagent/lib/node/index.js:905:3)
data: app.js:746160 - at IncomingMessage.<anonymous> (/home/ubuntu/hlf-client-app/node_modules/superagent/lib/node/index.js:1127:20)
data: app.js:746160 - at Stream.emit (events.js:400:28)
data: app.js:746160 - at Unzip.<anonymous> (/home/ubuntu/hlf-client-app/node_modules/superagent/lib/node/unzip.js:53:12)
data: app.js:746160 - at Unzip.emit (events.js:400:28)
data: app.js:746160 - at endReadableNT (internal/streams/readable.js:1334:12)
data: app.js:746160 - at processTicksAndRejections (internal/process/task_queues.js:82:21) {
data: app.js:746160 - body: {},
data: app.js:746160 - headers: {
data: app.js:746160 - 'cache-control': 'private, max-age=0',
data: app.js:746160 - 'access-control-allow-origin': '*',
data: app.js:746160 - 'access-control-allow-headers': 'Accept, App-Platform, Authorization, Content-Type, Origin, Retry-After, Spotify-App-Version, X-Cloud-Trace-Context, client-token, content-access-token',
data: app.js:746160 - 'access-control-allow-methods': 'GET, POST, OPTIONS, PUT, DELETE, PATCH',
data: app.js:746160 - 'access-control-allow-credentials': 'true',
data: app.js:746160 - 'access-control-max-age': '604800',
data: app.js:746160 - 'content-encoding': 'gzip',
data: app.js:746160 - 'strict-transport-security': 'max-age=31536000',
data: app.js:746160 - 'x-content-type-options': 'nosniff',
data: app.js:746160 - date: 'Sat, 17 Dec 2022 21:09:32 GMT',
data: app.js:746160 - server: 'envoy',
data: app.js:746160 - via: 'HTTP/2 edgeproxy, 1.1 google',
data: app.js:746160 - 'alt-svc': 'h3=":443"; ma=2592000,h3-29=":443"; ma=2592000',
data: app.js:746160 - connection: 'close',
data: app.js:746160 - 'transfer-encoding': 'chunked'
data: app.js:746160 - },
data: app.js:746160 - statusCode: 504
data: app.js:746160 - }