简体   繁体   中英

Handling user authorization with Instagram API

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: https://github.com/cosenary/Instagram-PHP-API .

My homepage logs the user in you can see the php code below

<?php

require 'Instagram.php';
use MetzWeb\Instagram\Instagram;

// initialize class
     $instagram = new Instagram(array(
     'apiKey'      => 'YOUR_APP_KEY',
     'apiSecret'   => 'YOUR_APP_SECRET',
     'apiCallback' => 'YOUR_APP_CALLBACK' // must point to success.php
     ));

// create login URL
 $loginUrl = $instagram->getLoginUrl();

?>

Users are successfully sent to the Instagram login page but when they authorize my app and Instagram responds with a http://your-redirect-uri?code=CODE The next page fails to render.

You can see the code for the "success" page that is supposed to be displayed below.

/**
* 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'      => 'YOUR_APP_KEY',
'apiSecret'   => 'YOUR_APP_SECRET',
'apiCallback' => '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->getOAuthToken($code);
$username = $username = $data->user->username;

// store user access token
$instagram->setAccessToken($data);

// now you have access to all authenticated user methods
$result = $instagram->getUserMedia();

} else {

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

}

?>

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.

Check that YOUR_APP_CALLBACK redirect url is correct - it should be the same here as it is in your Instagram App. Check out their guidelines for acceptable redirect urls here: http://instagram.com/developer/authentication/

As long as $_GET['code']; is set then the plugin should take care of the rest so I would check that this is being set too.

If you haven't done this already have a look at the plugin example here . That should get you started.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM