简体   繁体   中英

How to get images from my own Instagram with PHP?

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.

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.

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 基本显示

  1. Within the section Basic Display of the Instagram Basic Display product you will find your App ID and Secret 应用程序 ID 和机密

  2. Below that you can enter your redirect_uri

重定向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

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