Question

I'm trying to create a instagram app using the php library which is here(its not very long file, don't worry):

http://pastie.org/3391787

I'm trying to authorise the user by using OAUTH, which works perfectly, and the authorised(current user) can see his information(like Image, name etc...), but when i refresh the page it all goes and I'm left with blank data, heres the code -

// Instantiate the API handler object
$instagram = new Instagram($config);
$accessToken = $instagram->getAccessToken();
$_SESSION['InstagramAccessToken'] = $accessToken;

$instagram->setAccessToken($_SESSION['InstagramAccessToken']);
$userinfo = $instagram->getUser($_SESSION['InstagramAccessToken']);

// After getting the response, let's iterate the payload
$ures = json_decode($userinfo, true);

this code works fine the first time round, but when you refresh the page, the information is not being retrieved, and that could only mean one thing, that the session or the cookie is not set properly and thats why the getUser method is not retrieving the information,

$userinfo = $instagram->getUser($_SESSION['InstagramAccessToken']);

please can you shed some light into this problem, i have been trying for 4 hours!!

Was it helpful?

Solution

On each your request you overwrite current session data with nothing by this line:

$_SESSION['InstagramAccessToken'] = $accessToken;

Most likely this is how it should look like:

$instagram = new Instagram($config);

if (!empty($_SESSION['InstagramAccessToken'])) {
    $accessToken = $instagram->getAccessToken();
    $_SESSION['InstagramAccessToken'] = $accessToken;
} else {
    $instagram->setAccessToken($_SESSION['InstagramAccessToken']);
}

$userinfo = $instagram->getUser($_SESSION['InstagramAccessToken']);

// After getting the response, let's iterate the payload
$ures = json_decode($userinfo, true);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top