400 bad request from POST request

I fixed the redirect_uris to be matching, and now i’m experiencing a different issue. I rewrote my code so that I could access a reponse.body and follow the errors. Now i’m having the error

{“status”:400,“message”:“Invalid authorization code”}.

From what I’ve read on your posts in the past, this is usually due to making 2 post requests, but idk what i’d be doing that would cause that.

here’s what i’m working with now-
def create

session_code = params[:code]

link = "https://id.twitch.tv/oauth2/token"

headers = {

  "client_id" => "#{ENV["id"]}",

  "client_secret" => "#{ENV["secret"]}",

  "code" => "#{session_code}",

  "grant_type" => "authorization_code",

  "redirect_uri" => "http://localhost:3001/auth/twitch/callback"

}

url = URI(link)

http = Net::HTTP.new(url.host, url.port)

http.use_ssl = true

response = http.post(url.path, URI.encode_www_form(headers))

puts JSON.parse(response.body)
#returns {"status"=>400, "message"=>"Invalid authorization code"}

access_token = JSON.parse(response)["access_token"]

session[:token] = access_token

client = Twitch::Client.new(:client_id => "#{ENV["id"]}",:client_secret => "#{ENV["secret"]}", token_type: :user)

puts client.access_token

user = client.get_users(access_token: access_token).data.first

@profile_data = { :image => user.profile_image_url, :name => user.display_name, :twitch_id => user.id }

if User.find_by(@profile_data) == nil

  @profile = User.new(@profile_data)

  @profile.save

else

  @profile = User.find_by(@profile_data)

end

@profile_data = session[:profile_data] 

end

it’s also worth noting that if i print

URI.encode_www_form(headers)

to the console, it returns

client_id=<redacted>&client_secret=<redacted>&code=fr03l<redacted>83wy&grant_type=authorization_code&redirect_uri=http%3A%2F%2Flocalhost%3A3001%2Fauth%2Ftwitch%2Fcallback

which seems like it should be correct to me

also, the OG link is

const redirect = “http://localhost:3001/auth/twitch/callback

const url = 'https://id.twitch.tv/oauth2/authorize? 
response_type=code&client_id=' + clientId + '&redirect_uri=' + redirect + '&scope=user:read:email';