Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Instagram Basic API: Is it possible to get the media_url from "CAROUSEL_ALBUM" in one query?

If you are reading this, you will read that you can add children to the fields query parameter. That means that if you have a media with the type "CAROUSEL_ALBUM" you will get the ids of the images as well.

Example CURL:

https://graph.instagram.com/me?access_token=<mytoken>&fields=id,media_type,media_url,children

The result:

...
     "id": "some-id",
     "media_type": "CAROUSEL_ALBUM",
     "media_url": "some-url",
     "children": {
        "data": [
           {
              "id": "another-id"
           },
           {
              "id": "other-id"
           }
        ]
     }
...

Is it possible to add media_url to the children's data? I don't really want to fetch everything in a loop...

like image 524
Philipp Mochine Avatar asked Nov 06 '19 14:11

Philipp Mochine


People also ask

What data can I get from Instagram API?

The Instagram Basic Display API allows users of your app to get basic profile information, photos, and videos in their Instagram accounts. The API can be used to access any type of Instagram account but only provides read-access to basic data.

How do you get the basic display API access token on Instagram?

1. You can access the token generator in the App Dashboard > Products > Instagram > Basic Display tab. 2. Scroll down to User Token Generator and click on Generate Token next to your account.


1 Answers

@Mecha I did this for PHP, but maybe you can read this


    /**
     * Fetches media from the user
     *
     */
    public function fetchUserMedia(array $options, int $limit = 24): object
    {
        $query = http_build_query(array_merge([
            'limit' => $limit,
            'fields' => 'username,caption,id,media_type,media_url,thumbnail_url,children{media_url,thumbnail_url}',
            'access_token' => $this->token
        ], $options));


        return json_decode((string) ($this->client->get("https://graph.instagram.com/me/media?{$query}"))->getBody());
    }

One output example is like this:

{
          "caption": "I\u2018m addicted to chia pudding, how about you? \ud83d\ude0d Layers of zingy lemon vanilla chia pudding, mango thyme infused coconut yoghurt and juicy fresh mango roses. \ud83e\udd24\nMade by \u0040addictedtodates \ud83c\udf4b\u2063\u2063\n\u2063\u2063\u2800\nAND now to our tagged picture of today \ud83d\ude0b\ud83d\ude0b Swipe left to see \u0040smoothie_yumm\u2019s coconut vanilla smoothie bowl topped with turmeric chia pudding \ud83e\udd29\n\u2800\nYou can find the recipe on her page \ud83d\udc9b\n\u2800\n\ud83c\udf31 Tag #avidofood or\u00a0\u0040avidofood\u00a0to be featured in the future!\n\u2800\n\u22c6\u22c6\u22c6\u22c6\u22c6\u22c6\u22c6\u22c6\u22c6\u22c6\u22c6\u22c6\u22c6\u22c6\u22c6\u22c6\u22c6\u22c6\u22c6\u22c6\u22c6\u22c6\u22c6\u22c6\u22c6\u22c6\u22c6\u22c6\u22c6\u22c6\u22c6\u22c6\u22c6\u22c6\u22c6\u22c6\u22c6\u22c6\u22c6\n#vegan #veganfood #veganfoodie #veganfoodshare #vegatarisch #veganpasta #vegandinner #veganeats #healthyfood #plantbased #plantbasedfood #veganpower #plantbaseddiet #veganiseasy #whatveganseat #forksoverknives #vegetarianrecipes #veganinspo #vegetarian #veganism #veganmeals #veganlife #veganlifestyle #veganlove #veganmealprep #veganrecipes #veganhealth #veganlunch",
          "id": "18039820117203997",
          "media_type": "CAROUSEL_ALBUM",
          "media_url": "https://scontent.xx.fbcdn.net/v/t51.2885-15/71511404_203552310658678_3865693276191599368_n.jpg?_nc_cat=100&_nc_oc=AQlet8stFGS32TTdBhXT4NNfpzd8eNq7oI0xilix4qyiVvt50avuk6RVotMgM-BUptmCrsVwLCGkPCc-sL7b-eAy&_nc_ht=scontent.xx&oh=f1a700b4d021d2d577d54cd74f4838fa&oe=5E534677",
          "permalink": "https://www.instagram.com/p/B3-XRMOIWPW/",
          "username": "avidofood",
          "children": {
             "data": [
                {
                   "media_url": "https://scontent.xx.fbcdn.net/v/t51.2885-15/71511404_203552310658678_3865693276191599368_n.jpg?_nc_cat=100&_nc_oc=AQlet8stFGS32TTdBhXT4NNfpzd8eNq7oI0xilix4qyiVvt50avuk6RVotMgM-BUptmCrsVwLCGkPCc-sL7b-eAy&_nc_ht=scontent.xx&oh=f1a700b4d021d2d577d54cd74f4838fa&oe=5E534677",
                   "id": "18041124712207922"
                },
                {
                   "media_url": "https://scontent.xx.fbcdn.net/v/t51.2885-15/72483636_1251439178368296_6758852942587086206_n.jpg?_nc_cat=109&_nc_oc=AQmVrktP2g7Z72WifVdu4z17OzwM7ZNFLln1e1ZQxjdUi-j79Ttf-i840mjYkOb-TW3Dwm39Gyoe3EefxwB7UydW&_nc_ht=scontent.xx&oh=a07d3f51aa5eb5eb30697c4ad25d4e35&oe=5E60AEC3",
                   "id": "17851162897631131"
                }
             ]
          }
like image 123
Philipp Mochine Avatar answered Oct 10 '22 10:10

Philipp Mochine