twitch Exchange the code for the OAuth token


Example

Now that you have an authorization code, you can make a POST to the token endpoint (https://api.twitch.tv/kraken/oauth2/token) to get an OAuth token. You will receive a JSON-encoded access token, refresh token, and a list of the scopes approved by the user. You can now use that token to make authenticated requests on behalf of the user.

<?php
  $authCode = $_GET['code'];

  $parameterValues = array(
    'client_id' => '...',
    'client_secret' => '...',
    'grant_type' => 'authorization_code',
    'redirect_uri' => 'http://localhost/',
    'code' => $authCode
  );

  $postValues = http_build_query($parameterValues, '', '&');

  $ch = curl_init();
    
  curl_setopt_array($ch, array(
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_URL => 'https://api.twitch.tv/kraken/oauth2/token',
    CURLOPT_POST => 1,
    CURLOPT_POSTFIELDS => $postValues
  ));
            
  $response = curl_exec($ch);
  curl_close($ch);

  echo $response;
?>