title: Handling user authorization with Instagram API
body: <p>I am building a pretty simple web application. I'm having trouble handling the code return after a user logs in with Instagram. I'm also using another API to assist me you can find it here: <a href="https://github.com/cosenary/Instagram-PHP-API" rel="nofollow">https://github.com/cosenary/Instagram-PHP-API</a>. </p>
  
  <p>My homepage logs the user in you can see the php code below</p>
  
  <pre><code>&lt;?php
  
  require 'Instagram.php';
  use MetzWeb\Instagram\Instagram;
  
  // initialize class
       $instagram = new Instagram(array(
       'apiKey'      =&gt; 'YOUR_APP_KEY',
       'apiSecret'   =&gt; 'YOUR_APP_SECRET',
       'apiCallback' =&gt; 'YOUR_APP_CALLBACK' // must point to success.php
       ));
  
  // create login URL
   $loginUrl = $instagram-&gt;getLoginUrl();
  
  ?&gt;
  </code></pre>
  
  <p>Users are successfully sent to the Instagram login page but when they authorize my app and Instagram responds with a <code>http://your-redirect-uri?code=CODE</code> The next page fails to render. </p>
  
  <p>You can see the code for the "success" page that is supposed to be displayed below. </p>
  
  <pre><code>/**
  * Instagram PHP API
  *
  * @link https://github.com/cosenary/Instagram-PHP-API
  * @author Christian Metz
  * @since 01.10.2013
  */
  
  require_once 'Instagram.php';
  use MetzWeb\Instagram\Instagram;
  
  // initialize class
  $instagram = new Instagram(array(
  'apiKey'      =&gt; 'YOUR_APP_KEY',
  'apiSecret'   =&gt; 'YOUR_APP_SECRET',
  'apiCallback' =&gt; 'YOUR_APP_CALLBACK' // must point to success.php
  ));
  
  // receive OAuth code parameter
  $code = $_GET['code'];
  
  // check whether the user has granted access
  if (isset($code)) {
  
  // receive OAuth token object
  $data = $instagram-&gt;getOAuthToken($code);
  $username = $username = $data-&gt;user-&gt;username;
  
  // store user access token
  $instagram-&gt;setAccessToken($data);
  
  // now you have access to all authenticated user methods
  $result = $instagram-&gt;getUserMedia();
  
  } else {
  
  // check whether an error occurred
  if (isset($_GET['error'])) {
  echo 'An error occurred: ' . $_GET['error_description'];
  }
  
  }
  
  ?&gt;
  </code></pre>
  
  <p>If users cancel the authorization request the "success" page properly renders and displays the correct error message. So I believe my issue is in the handling of the code parameter that Instagram returns to my web app. Thanks for the help. </p>