Announcements

Help Wizard

Step 1

NEXT STEP

client credentials not refreshing token

client credentials not refreshing token

 

Device

 

Operating System

Mac

 

My Question or Issue

I am attempting to use the web-api-ts-sdk inside a Nextjs server action. I am using the withClientCredentials(), which I would assume I would not need to refresh the access token. But I get the following error when trying to use the client, after successfully creating the client: 

"Bad or expired token. This can happen if the user revoked a token or the access token has expired. You should re-authenticate the user."

I am just trying to search something with the api, so not accessing user data.


export const createClient = async () => {
const cl = SpotifyApi.withClientCredentials(
process.env.SPOTIFY_CLIENT_ID ?? "",
process.env.SPOTIFY_CLIENT_SECRET ?? "");
return cl;
}

 

 

 

Reply
1 Reply

Is that createClient fn within 'use server' file? Or do you import it into a 'use server' file and call it from within a server action definition?

 

I don't use server actions myself too often, but it might be a good idea to have the SDK client creation within the server action itself and not a separate function. That way it's always encapsulated in the server action execution and gets a fresh access token every time. Next.js can be funky sometimes

```

"use server"

 

export const MyServerAction = async () => {

// Start client within server action, not separate

const cl = await SpotifyApi.withClientCredentials(args...);

// Do stuff with client

return;

}

```

Suggested posts

Let's introduce ourselves!

Hey there you,   Yeah, you! 😁   Welcome - we're glad you joined the Spotify Community!   While you here, let's have a fun game and get…

ModeratorStaff / Moderator/ 4 years ago  in Social & Random