Announcements
From 28th September 2026, Community private messages will no longer be available. If you have any private messages you'd like to keep, please save or copy them using these steps before this date. Thank you.

Help Wizard

Step 1

NEXT STEP

Authorization code flow: get access token error invalid client_id

Authorization code flow: get access token error invalid client_id

Hey guys ! 

I'm building a webapp based on Spotify API using RoR. To get the access token, I use the Autorization Code Flow process.

 

At step 2, I when make the [POST] to https://accounts.spotify.com/api/token, sending my client_id and client_secret base64 encoded, I have a 400 "Invalid client_id".
Then I tried to sending it in the headers and this time I have a 400 "Invalid authorization_code".

 

I'm a bit lost right now, your help is greatly appreciated 🙂

 

 

  def get_token
    api_endpoint = 'https://accounts.spotify.com/api/token'
    encoded_auth = Base64.encode64(ENV['SPOTIFY_CLIENT'] + ENV['SPOTIFY_SECRET']).delete("\n")

    body = {
      grant_type: 'authorization_code',
      code: params['code'],
      redirect_uri: "http://localhost:3000/auth/spotify/callback",
      client_id: ENV['SPOTIFY_CLIENT'],
      client_secret: ENV['SPOTIFY_SECRET']
    }
    headers = { Authorization: "Authorization: Basic #{encoded_auth}" }

    resp = HTTParty.post(
      api_endpoint,
      headers: headers,
      body: body
    )
  end

 

 

Reply
1 Reply

Shouldn't you have separated the client ID and client secret with a colon character? And why is it prefixed with a hash character?

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/ 5 years ago  in Social & Random