Parameter redirect_uri does not match registered URI

Hi guys,

maybe someone can help me out what I am doing wrong? I tried the authentification workflow as follows in php:

  define('AUTH_LOGINURL', '');
  define('AUTH_TOKENURL', '');
  define('AUTH_CLIENTID', 'myclientid');
  define('AUTH_REDIRECTURL', '');
  define('AUTH_SCOPES', 'user_read');
  define('AUTH_SECRET', 'mysecret');
  if(isset($_GET['code'])) {
    $data = array(
      'client_id' => urlencode(AUTH_CLIENTID),
      'client_secret' => urlencode(AUTH_SECRET),
      'grant_type' => urlencode('authorization_code'),
      'redirect_uri' => urlencode(AUTH_REDIRECTURL),
      'code' => urlencode($_GET['code']),
      'state' => urlencode($_GET['state'])
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, AUTH_TOKENURL);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
    curl_setopt($ch,CURLOPT_POST, 1);
    curl_setopt($ch,CURLOPT_POSTFIELDS, $data);
    $result = curl_exec($ch);
    echo $result; 
  } else {
    echo '<a href="'.AUTH_LOGINURL.'?response_type=code&client_id='.urlencode(AUTH_CLIENTID).'&redirect_uri='.urlencode(AUTH_REDIRECTURL).'&scope='.AUTH_SCOPES.'&state='.time().'"><img src="connect_dark.png"/></a>';


Each time i am trying this, i got the following result:

My browser gets refreshed to following url:

but the output from the echo while fetching the access token is still:

{"error":"Bad Request","status":400,"message":"Parameter redirect_uri does not match registered URI"}

Why does that happen?

Did you set the redirect_uri to in the Application Settings on Twitch?

Found the issue… The POST parameters should NOT be urlencoded as it is mentioned in the API doc.

CURLOPT_POSTFIELDS does the urlencoding for you if you pass the data as an array, so you were in fact double-encoding it.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.