Hello,
I’m currently building a Rails application with this Twitch gem. When I attempt to log in, I am correctly redirected to Twitch and back to my redirect URI, but attempting to get an access token with the provided auth code always fails.
Here’s the code on my redirect_uri (identical to the example provided in the gem’s README):
@twitch = Twitch.new({
:client_id => CLIENT_ID,
:secret_key => SECRET_KEY,
:redirect_uri => REDIRECT_URI
})
@data = @twitch.auth(params[:code])
session[:access_token] = @data[:body]["access_token"]
As a quick side note, here are the relevant lines from the twitch gem:
def initialize(options = {})
@client_id = options[:client_id] || nil
@secret_key = options[:secret_key] || nil
@redirect_uri = options[:redirect_uri] || nil
@scope = options[:scope] || nil
@access_token = options[:access_token] || nil
@base_url = "https://api.twitch.tv/kraken"
end
def auth(code)
path = "/oauth2/token"
url = @base_url + path
post(url, {
:client_id => @client_id,
:client_secret => @secret_key,
:grant_type => "authorization_code",
:redirect_uri => @redirect_uri,
:code => code
})
end
The parameters passed to my redirect_uri (auth code has been modified for privacy):
{"code"=>"rfyyvhb4jvcbd3t6wrcn12v24ybnzl6", "scope"=>"user_read"}
The API always responds with:
{:body=>{"error"=>"Bad Request", "status"=>400, "message"=>"Invalid authorization code"}, :response=>400}
Thanks!