Announcements

Help Wizard

Step 1

NEXT STEP

FAQs

Please see below the most popular frequently asked questions.

Loading article...

Loading faqs...

VIEW ALL

Ongoing Issues

Please see below the current ongoing issues which are under investigation.

Loading issue...

Loading ongoing issues...

VIEW ALL

Error processing Spotify callback: 5 NOT_FOUND:

Error processing Spotify callback: 5 NOT_FOUND:

Developing an application that pulls artist data. But I can even get to that point. Im trying to connect to authenticate with firebase and Im getting this error proccessing Spotify callback: 5 NOT_FOUND.

Ive been trying for days and cant get past this error.

has any one seen this?

My Oauth js

const functions = require('firebase-functions');
const admin = require('firebase-admin');
const { getSpotifyAuthUrl, exchangeCodeForToken } = require('./api_integrations/spotify');

// Initialize Firebase Admin SDK if not already initialized
if (admin.apps.length === 0) {
admin.initializeApp();
}

const db = admin.firestore();

/**
* Initiates the Spotify OAuth 2.0 authorization flow.
* Redirects the user to Spotify's authorization page.
*/
exports.spotifyAuthRedirect = functions.https.onRequest(async (req, res) => {
try {
const { labelId } = req.query; // Expect labelId to associate the token
if (!labelId) {
functions.logger.error('spotifyAuthRedirect: Missing labelId query parameter.');
return res.status(400).send('Missing labelId query parameter.');
}

// Store labelId in session or pass it through state for callback
// For simplicity, we'll pass it in the state parameter (needs to be URL encoded)
const state = `labelId=${labelId}`;
const authorizationUrl = getSpotifyAuthUrl(state);
functions.logger.info(`spotifyAuthRedirect: Redirecting to Spotify for labelId: ${labelId}`);
return res.redirect(authorizationUrl);
} catch (error) {
functions.logger.error('spotifyAuthRedirect: Error generating Spotify auth URL', error);
return res.status(500).send('Failed to initiate Spotify authorization.');
}
});

/**
* Handles the callback from Spotify after user authorization.
* Exchanges the authorization code for an access token and refresh token,
* then stores them securely (e.g., in Firestore, associated with the labelId).
*/
exports.spotifyAuthCallback = functions.https.onRequest(async (req, res) => {
const { code, state, error: oauthError } = req.query;

if (oauthError) {
functions.logger.error('spotifyAuthCallback: OAuth error from Spotify:', oauthError);
return res.status(400).send(`OAuth Error: ${oauthError}`);
}

if (!code) {
functions.logger.error('spotifyAuthCallback: No authorization code provided by Spotify.');
return res.status(400).send('Authorization code missing.');
}

// Extract labelId from state
let labelId;
if (state) {
const params = new URLSearchParams(state);
labelId = params.get('labelId');
}

if (!labelId) {
functions.logger.error('spotifyAuthCallback: Missing labelId in state parameter.');
return res.status(400).send('State parameter missing or invalid (labelId not found).');
}

try {
functions.logger.info(`spotifyAuthCallback: Received code for labelId: ${labelId}. Exchanging for token.`);
const tokenData = await exchangeCodeForToken(code);

if (tokenData && tokenData.access_token) {
functions.logger.info(`spotifyAuthCallback: Token received for labelId: ${labelId}. Storing...`);
// Securely store the tokens (e.g., in Firestore, associated with the labelId)
// This is a critical part: ensure tokens are encrypted or stored with strict access controls.
// For this example, we'll store it in a 'serviceCredentials' subcollection under the 'labels' collection.
const labelRef = db.collection('labels').doc(labelId);
await labelRef.collection('serviceCredentials').doc('spotify').set({
accessToken: tokenData.access_token,
refreshToken: tokenData.refresh_token,
expiresIn: tokenData.expires_in,
scope: tokenData.scope,
tokenType: tokenData.token_type,
retrievedAt: admin.firestore.FieldValue.serverTimestamp()
});

functions.logger.info(`spotifyAuthCallback: Spotify tokens stored successfully for labelId: ${labelId}`);
// Redirect user to a success page or back to the app
return res.status(200).send('Spotify authorization successful! Tokens stored. You can close this window.');
} else {
functions.logger.error('spotifyAuthCallback: Failed to exchange code for token or token data is invalid.', tokenData);
return res.status(500).send('Failed to obtain Spotify token.');
}
} catch (err) {
functions.logger.error('spotifyAuthCallback: Error processing Spotify callback for labelId:', labelId, err);
return res.status(500).send('Internal server error during Spotify callback processing.');
}
});
Reply
0 Replies

Suggested posts

Type a product name