Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot Store Instagram API access_token with PHP

I'm working on building a small web app connecting into the instagram API. Currently using this library on GitHub which makes things a bit easier. I can connect and initially login fine, and the page will display all user data.

But once you refresh the page all the data is lost, and it appears the script can't find my access token anymore. I tried storing this into a PHP session variable - but maybe I'm doing the whole process incorrectly? I just want to keep the same user session throughout an entire website once the OAuth is performed.

You can check out the small app live here: http://spyrestudios.com/demos/instagram-api/index.php

Additionally my callback URL is http://spyrestudios.com/demos/instagram-api/instagammy.php - this is the script which will work right after you connect. But try refreshing the page and all the data is gone! Also here is my bit of code which *should use the library to store the current user's access token:

session_start();
require_once 'Instagram.php'; // the library code
$config = array(
    'client_id' => 'f0d225aa955c4bd9ae563f87f831efab', // Your client id
    'client_secret' => '377b77afc1274a89bd2df7d77e934689', // Your client secret
    'grant_type' => 'authorization_code',
    'redirect_uri' => 'http://spyrestudios.com/demos/instagram-api/instagammy.php', // The redirect URI you provided when signed up for the service
 );
// Instantiate the API handler object
$instagram = new Instagram($config);
$accessToken = $instagram->getAccessToken();
$_SESSION['InstagramAccessToken'] = $accessToken;

Really struggling for a solution, so I'd appreciate any help I can get. Willing to post examples of my code if needed..

Thanks in advance!

like image 997
Jake Avatar asked Mar 16 '26 14:03

Jake


1 Answers

What you have already looks fine (although I have no experience in Instagram), assuming that $accessToken is actually being stored correctly in the session.

But surely at some point you need to feed back in $accessToken so that it can be verified. Like I said, I've not used Instagram before, so I don't know how you would do this.

like image 75
VettelS Avatar answered Mar 19 '26 02:03

VettelS