I am encountering a consistent issue when attempting to access the Spotify API using accounts that have been created through or linked with Google. The login flow works perfectly for Spotify accounts with direct credentials, but when using Google as an authentication provider, the process results in an HTTP 403 Forbidden error.
Here's a brief outline of the problem:
Environment: Node.js server application using the spotify-web-api-node library.
Flow Used: Authorization Code Flow, as recommended for server-side applications.
Issue: The API call to spotifyApi.getMe() throws an error after successfully obtaining the access and refresh tokens. This only occurs when the user logs in through Google, not with Spotify's native credentials.
Error Message: "Error getting Tokens: WebapiError: [object Object]" with a status code of 403.
Attempted Resolutions: Ensuring that the Redirect URI matches exactly as configured in the Spotify Developer Dashboard, confirming that the CLIENT_ID and CLIENT_SECRET are correctly set, and checking the requested scopes. The issue persists despite these checks.
Here is my callback function
app.get('/callback', async (req, res) => {
const { code } = req.query;
try {
const data = await spotifyApi.authorizationCodeGrant(code);
const { access_token, refresh_token, expires_in } = data.body;
console.log('Access Token:', access_token);
console.log('Refresh Token:', refresh_token);
spotifyApi.setAccessToken(access_token);
spotifyApi.setRefreshToken(refresh_token);
const me = await spotifyApi.getMe();
res.send(me.body);
} catch (error) {
console.error('Error getting Tokens:', error.statusCode, JSON.stringify(error.message));
console.dir(error);
console.error('Error getting Tokens:', error);
res.status(500).send({
error: 'Error occurred while getting tokens from Spotify',
details: error.response && error.response.body ? error.response.body : error
});
}
});