0

Main Aim: To get access_token so I may display my Instagram feed on the website.

Currently, to get an access token, we go to this url and click on Authorize to get the code, further exchanged for access_token.

 https://api.instagram.com/oauth/authorize
  ?client_id={app-id}
  &redirect_uri={redirect-uri}
  &scope=user_profile,user_media
  &response_type=code

Now, if I am writing a PHP-curl script to send this access_token and get my posts, how will I get an access token each time, I can not really click on authorize every time my API requests data, further, clicking authorize every 60 days is also not a long term solution for me.

So I was hoping if there was a way ( that I could call this URL, and get authorization code directly? )

So far my script:

$authURL = "https://api.instagram.com/oauth/authorize
?client_id=$client_id
&redirect_uri=$redirect_uri
&scope=user_profile,user_media
&response_type=code";

//STEP 1, GET THE AUTHORIZATION CODE (i am stuck here, how to get code from only PHP, 
//without external clicks..)
//https://www.redirecturl.com?code=AQgw#_

$authorization_code = '48FduaX0g.....VZIj';

//STEP 2 GIVE THE CODE FOR TOKEN

$url = 'https://api.instagram.com/oauth/access_token';
$myjson = "
            {'client_id': $client_id,
             'client_secret': $client_secret,
             'grant_type':'authorization_code',
             'redirect_uri':$redirect_uri,
             'code': $authorization_code
            }";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $myjson);
$result = curl_exec($ch);
curl_close($ch);

$insta_details = json_decode($result);
echo $insta_details['access_token'];


curl -X GET \
  'https://graph.instagram.com/17841405793187218/media?access_token=IGQVJ...'
Div
  • 63
  • 2
  • 10

1 Answers1

1

The authorization step requires human interaction to login with a username / password. So there is no way to do that via only PHP.

But if you just need to display a feed on a website using PHP, you can use this package: https://packagist.org/packages/espresso-dev/instagram-basic-display-php

Grab the code in the callback after auth:

$code = $_GET['code'];

And then use this following methods:

  • getOAuthToken() to het the initial token
  • getLongLivedToken() to get the long-lived token valid for 60 days

Store the long-lived token so that can be used to retrieve the posts and just call the refreshToken() method every 50 days or so to refresh the token in the background and update the token you're using.

hermanschutte
  • 662
  • 2
  • 5
  • 20
  • can I grab the code without using echo "Login with Instagram"; .. i mean my website visitor would have to click on this anchor to view the posts right? I want visitors to see these posts on the homepage.. without the need of such interaction.. – Div Jan 31 '20 at 05:58
  • Your own posts, or the visitors posts? If it's for your own posts, then no, they would not need to log in with their Instagram accounts. You would be using your own accounts token to display the feed. – hermanschutte Jan 31 '20 at 09:00
  • want to show my own posts on my own website. Yes, so how would I get these tokens? After every 60 days, I must keep regenerating them by clicking authorize? – Div Jan 31 '20 at 11:38
  • 1
    No, you can use the refresh endpoint mentioned above and in the docs: https://developers.facebook.com/docs/instagram-basic-display-api/guides/long-lived-access-tokens. – hermanschutte Jan 31 '20 at 13:13
  • I'm currently trying to implement this on my website and I just have to get it off my chest that WOW did Facebook/Instagram make it really difficult to retrieve data that is already publicly visible (if the profile isn't private). It's amazingly complex for something so simple. I've been googling for hours trying to find a way around having to store and refresh a long-lived token, but it does not seem like there is one. – Soulfire Feb 10 '20 at 21:45
  • Hi @Soulfire I have achieved this, let me know if you still need any help. – Div Feb 17 '20 at 10:44
  • That would be helpful if you are willing to share example code - perhaps ask and answer a question on here? I would gladly upvote it! – Soulfire Feb 24 '20 at 18:54
  • 1
    https://github.com/Divya-Singal/instagram-basic-display-php Please follow this, if it works for you, then upvote this question! @Soulfire – Div Mar 09 '20 at 12:17