Announcements

Help Wizard

Step 1

NEXT STEP

Refreshing the access token PKCE Flow

Refreshing the access token PKCE Flow

Country

Denmark (DK)

Device

Desktop

Operating System

Windows 10

 

My Question or Issue

I am trying to do a post request for a new access token / refresh the access token with this JS code:

 

const refreshAccessToken = async () => {
  const refreshToken = localStorage.getItem('refresh_token');
  try {

    const body = new URLSearchParams({
      grant_type: 'refresh_token',
      refresh_token: refreshToken,
      client_id: clientId,
      client_secret: client_Secret
    }).toString();
    const response = await fetch('https://accounts.spotify.com/api/token', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      body: body
    });

      if (!response.ok) {
        throw new Error('HTTP status ' + response.status);
      }

      const data = await response.json();
      localStorage.setItem('access_token', data.access_token);
  } catch (error) {
    console.error('Error refreshing access token:', error);
  }
};

but im getting this Error: HTTP status 400 so something in this part of the code must be wrong: 

const body = new URLSearchParams({
      grant_type: 'refresh_token',
      refresh_token: refreshToken,
      client_id: clientId,
      client_secret: client_Secret
    }).toString();
    const response = await fetch('https://accounts.spotify.com/api/token', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      body: body
    });
 
can't find the problem... 
i appreciate the help! 
 
Reply
1 Reply

Howldy Ulrick1432! 
It looks like you're on the right track, but there might be an issue with the parameters you're sending in your POST request. Double-check that your `refreshToken`, `clientId`, and `client_Secret` values are correct and not empty. Also, ensure that the `Content-Type` header is set correctly as `'application/x-www-form-urlencoded'`. If all the values are accurate and you’re still encountering issues, let me know and we’ll work this out.

Not to be hounding you.. but keep me in the loop!

 

-Prague the Dog

PragueRising Star
Help others find this answer and click "Accept as Solution".
If you appreciate my answer, maybe give me a Like.
Note: I'm not a Spotify employee.

Suggested posts