0

can somebody tell me please how can I get images from my own Instagram via PHP? I have login name, password, app id, app secret. IG changed the api so all the documentation including how to get access token does not work. I have this peace of code:

$fb = new \Facebook\Facebook([
    'app_id' => self::APP_ID_IG,
    'app_secret' => self::APP_SECRET_IG,
    'default_graph_version' => 'v2.10',
]);

// This return string which creates access token as "app_id value|app_secret value"
$token = $fb->getApp()->getAccessToken();

// This line throws me an error: 
// Error validating application. Cannot get application info due to a system error.
$response = $fb->get('https://graph.instagram.com/me/media?fields=media_url,media_type', $token->getValue());

I am lost in it. Thanks for help.

Čamo
  • 3,863
  • 13
  • 62
  • 114

2 Answers2

1

You should take a look at the Instagram's Media API

This endpoint should work to query the user's media: GET /{ig-user-id}/media giving you a list of media ids:

{
  "data": [
    {
      "id": "17895695668004550"
    },
    {
      "id": "17899305451014820"
    },
    {
      "id": "17896450804038745"
    },
    {
      "id": "17881042411086627"
    },
    {
      "id": "17869102915168123"
    }
  ]
}

After that, I think you sould use the graph API to get the image URL of each media id.

Dev_NIX
  • 163
  • 4
  • 18
  • And how to get user-id if he is not logged in? – Čamo Nov 09 '20 at 12:09
  • I found user-id so i am going to try it. – Čamo Nov 09 '20 at 12:13
  • No it does not work. Every request to graph api needs access token. Also this one. – Čamo Nov 09 '20 at 12:16
  • Yeah, doesn't work like this? `$response = $fb->get('https://graph.instagram.com/{user-id}/media', $token->getValue());` – Dev_NIX Nov 09 '20 at 12:35
  • If not, let me know if this helps you, and I will update the answer: https://developers.facebook.com/docs/graph-api/reference/instagram-media/?locale=es_ES – Dev_NIX Nov 09 '20 at 12:40
  • Well, it seems like the `/me/media` is correct, but you have to ask permission to the user: https://developers.facebook.com/docs/instagram-basic-display-api/guides/getting-profiles-and-media Also maybe related: https://stackoverflow.com/questions/10183625/extending-facebook-page-access-token – Dev_NIX Nov 09 '20 at 12:44
  • I dont want to show login window. I need to do it at the background server to server. I have all credentials but cant connect to api. Still some kind of error. – Čamo Nov 09 '20 at 14:01
  • Well, in some moment you had to get the credentials. You should use that moment to request the correct permissions for your application: https://developers.facebook.com/docs/instagram-basic-display-api/guides/getting-access-tokens-and-permissions#step-1--get-authorization – Dev_NIX Nov 10 '20 at 06:42
  • I dont understand. I sould implement login window run it on the server then get access token for 60 days then remove login window from the code, then rewrite the code with saved token from login and then load it back to server? – Čamo Nov 11 '20 at 09:22
  • 1
    You should learn more about oauth2 authentication method. You do NOT need to create login windows. You should just show window generated by Facebook to end user to let him authorize your application with specific scopes – Urmat Zhenaliev Nov 11 '20 at 13:52
  • Or you could create application with https://oauth.net/2/grant-types/password. But I am not sure about this option for Facebook – Urmat Zhenaliev Nov 11 '20 at 13:56
0

I created a vuejs adaption a while back, maybe this code structure snippet helps you since I don't have time right now to translate it into php, it is written in javascript but the API requests remain the same (just use curl instead of axios): https://gitlab.com/-/snippets/1957175

General steps:

  1. Get your authorization code either via a Login Window or via a facebook app (described below)

  2. Transform it into a shortterm access token

curl --request POST \
  --url 'https://api.instagram.com/oauth/access_token?grant_type=authorization_code&client_id=[CLIENT_ID_HERE]&client_secret=[CLIENT_SECRET_HERE]&redirect_uri=[REDIRECT_URI_HERE]&code=[AUTHORIZATION_CODE_HERE]' \
  --header 'Content-Type: multipart/form-data' \
  --form client_id=[CLIENT_ID_HERE] \
  --form client_secret=[CLIENT_SECRET_HERE] \
  --form code=[AUTHORIZATION_CODE_HERE] \
  --form grant_type=authorization_code \
  --form redirect_uri=https://[REDIRECT_URI_HERE]/
  1. Transform that into a longterm access token
GET https://graph.instagram.com/access_token?grant_type=ig_exchange_token&access_token=[SHORTTERM_ACCESS_TOKEN_HERE]&client_secret=[CLIENT_SECRET_HERE]
  1. Query the Media Graph endpoint with the URL from my snippet using the longterm access token as authorization.
  2. Refresh that longterm access token every 60 days (see my answer on the topic on Github here). You could totally automate that step via for example via CRON.
GET https://graph.instagram.com/refresh_access_token?grant_type=ig_refresh_token&access_token=[OLD_ACCESS_TOKEN_HERE]

For more info about which field to add to the query check the official facebook documentation


Getting an authorization code via a facebook app:

  1. Log into your facebook developer account
  2. Create a new Application
  3. Under products, add Instagram Basic Display and fill out all necessary fields

Instagram Basic Display

  1. Within the section Basic Display of the Instagram Basic Display product you will find your App ID and Secret App ID and Secret

  2. Below that you can enter your redirect_uri

redirect_uri

  1. Finally below that is the User Token Generator which you can use to

[...]quickly generate long-lived Instagram User Access Tokens for any of your public Instagram accounts. This is useful if you [...] don’t want to bother with implementing the Authorization Window [...] See Instagram Basic Display API Docs

  1. Continue with 3. of General Steps
onewaveadrian
  • 434
  • 1
  • 6
  • 17
  • First step says: To get an Authorization Code, implement the Authorization Window into your app. After an app user authenticates their identity through the window and grants your app any permissions it needs, we will redirect the user to your app and include an Authorization Code.... I dont want to show any windows. I am the owner. I need the pictures without the authorisation windows. Server to server solution. – Čamo Nov 17 '20 at 19:10