Last active
July 31, 2020 06:19
-
-
Save ekafyi/36066424a79e155787d048b51cd3144b to your computer and use it in GitHub Desktop.
my attempt to adapt glitch.com/~spotify-web-api-node into Netlify functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// truncated version of my attempt to adapt https://glitch.com/~spotify-web-api-node into Netlify functions | |
const SpotifyWebApi = require("spotify-web-api-node"); | |
const TEST_ARTIST_ID = "1lYT0A0LV5DUfxr6doRP3d"; | |
let accessToken; | |
const spotifyApi = new SpotifyWebApi({ | |
clientId: process.env.SPOTIFY_CLIENT_ID, | |
clientSecret: process.env.SPOTIFY_CLIENT_SECRET, | |
}); | |
exports.handler = async (event) => { | |
if (!accessToken) { | |
const result = await spotifyApi.clientCredentialsGrant().then((data) => data); | |
accessToken = await result.body["access_token"]; | |
} | |
const spotifyAuthApi = new SpotifyWebApi({ accessToken }); | |
try { | |
data = await spotifyAuthApi.getArtist(TEST_ARTIST_ID); | |
return { | |
statusCode: 200, | |
body: JSON.stringify(data.body), | |
}; | |
} catch (err) { | |
return { | |
statusCode: 500, // temporary error code | |
}; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment