Instagram Redirect URI doesn’t match original redirect URI when using additional query parameters

I am working on a plugin for WordPress where a user clicks a “Sign in on Instagram” button to authorize my Instagram app. After authorization, the plugin will basically just get the latest Instagram photos from a user and display it via widget.

Here’s a step-by-step of how my plugin works:

Read More
  1. User clicks on “Sign in on Instagram” on a WordPress settings page.
  2. User will be taken to the
    Instagram’s Authorization screen (login screen)
  3. User successfully authenticates and approves my app.
  4. Instagram redirects the user to my Redirect URI.
  5. The file “instagram-api-redirect.php” will get both the “code” and “return_uri” parameter. Where the “code” is used for requesting the “access token”.
  6. It redirects back to the WordPress settings page along with the “access token”.
  7. The plugin stores the “access token” to the database to be used for authenticating requests.

What I am having a problem with is that I am getting the error message saying “Redirect URI doesn’t match original redirect URI” on Step 5. It works fine when I remove the “return_uri” query parameter from the Redirect URI.

Some details may help:

Here is my Redirect URI from my app:

http:// mysite.com/plugins/pulp_instagram/instagram-api-redirect.php

Here is my Redirect URI that I am sending to Instagram’s “/authorize”:

http:// mysite.com/plugins/pulp_instagram/instagram-api-redirect.php?return_uri=http:// localhost/instagram-app

Here is the full authorization URL i am sending to Instagram’s “/authorize”:

https:// api.instagram.com/oauth/authorize/?client_id=CLIENT-ID&redirect_uri=http:// mysite.com/plugins/pulp_instagram/instagram-api-redirect.php?return_uri=http:// localhost/instagram-app&response_type=code

Here’s the URL response:

http:// mysite.com/plugins/pulp_instagram/instagram-api-redirect.php?return_uri=http:// localhost/instagram-app&code=557d15dacd0d40459edf70aa159476de

This is the full code for the “instagram-api-redirect.php” file:

<?php 

// the redirect uri
$return_uri = $_GET['return_uri'];

require 'instagram.class.php';

// Initialize class
$instagram = new Instagram(array(
  'apiKey'      => 'CLIENT-ID',
  'apiSecret'   => 'CLIENT-SECRET',
  'apiCallback' => 'http://mysite.com/plugins/pulp_instagram/instagram-api-redirect.php'
));

// Receive OAuth code parameter
$code = $_GET['code'];

// Check whether the user has granted access
if (true === isset($code)) {

    // Receive OAuth token object
    $data = $instagram->getOAuthToken($code);
    print_r($data); 
} else {

  // Check whether an error occurred
  if (true === isset($_GET['error'])) {
    echo 'An error occurred: '.$_GET['error_description'];
  }

}
?>

Also,I am using the “Instagram PHP API” class (instagram.class.php) from cosenary.

Thanks in advance!

Related posts

Leave a Reply