'Instagram ?__a=1 url not working anymore & problems with graphql/query to get data

Update 19 April

After a few days using cookie ig_pr two days ago is block. Looks like the only way to get the data now is use sessionid with a specific value

Original

I was using instagram ?__a=1 url to read all the post of instagram's users.

A few hours ago there was a change in the response and now doesn't allow me to use max_id to paginate.

Before I usually sent a request to

https://www.instagram.com/{{username}}/?__a=1

and using the graphql.edge_owner_to_timeline_media.page_info.end_cursor in the response I called the same page with a new max_id

https://www.instagram.com/{{username}}/?__a=1&max_id={{end_cursor}}

Now the end_cursor changes in each call & max_id is not working.

Please help :)



Solution 1:[1]

Edit 15/03 NOT WORKING ANYMORE Seems like instagram changed again their API, now it gives a CORS error.

As of 2 february 2021, I have found a solution

Instead of using https://www.instagram.com/username/?__a=1 which it asks for a login.

Justing adding a /channel seems to make it work, like so:

https://www.instagram.com/username/channel/?__a=1

Solution 2:[2]

I just came by the same issue.

Looks like they just changed their private api by removing the max_id. Their website seems to have replaced the old api with the graphql api.

https://www.instagram.com/graphql/query/?query_hash=472f257a40c653c64c666ce877d59d2b&variables={"id":"111","first":12,"after":"xxx"}

  • id: user ID,
  • first: amount of nodes to get,
  • after: the 'end_cursor' you can get from data['user']['edge_owner_to_timeline_media']['page_info']['end_cursor']

use either query_hash or query_id

  • query_hash: 472f257a40c653c64c666ce877d59d2b
  • query_id: 17888483320059182

I have no idea how long that query_id/query_hash will work, it's up to Instagram. They will eventually change it.

Updated 4/8/2018 - Before FB didn't check any cookies, but looks like they added quick validation. Try adding ig_pr=2 to the request cookie, when sending your API. This quick fix works for me. Who knows when FB will change it again.

As long as FB doesn't enforce login for these basic APIs, there always will be an easy workaround.

Solution 3:[3]

Translated some of the folks' code to PHP:

<?php
function getPublicInfo($username) {
    $url     = sprintf("https://www.instagram.com/$username");
    $content = file_get_contents($url);
    $content = explode("window._sharedData = ", $content)[1];
    $content = explode(";</script>", $content)[0];
    $data    = json_decode($content, true);
    return $data['entry_data']['ProfilePage'][0];
}

Not sure for how long it's gonna work. For my small project it does the job for now. The result is very similar (if not equal) to the one at the URL: instagram.com/{user}/?__a=1

Solution 4:[4]

The main problem with using graph/query is that I only have the username, to extract the userId & the last post like we use to do with ?__a=1 we have to get the instagram's user page & extract _sharedData

Javascript

let url = "https://www.instagram.com/"+username;
$.ajax({
    type: 'GET',
    url: url,
    error: function () {
        //..
    },
    success: function (data) {
        data = JSON.parse(data.split("window._sharedData = ")[1].split(";</script>")[0]).entry_data.ProfilePage[0].graphql;
        console.log(data);
    }
})

After get all this data we can call graph/query (not in client side)

Solution 5:[5]

For pagination you can now use ?__a=1&page=2

Solution 6:[6]

This answer is not directly helping the question but posting because someone might benefit from the answer. As of the current date 12 April 2018, the load more APIs will not work without a Cookie header set. Below are some codes for fetching Instagram public APIS

    let url = "https://www.instagram.com/explore/";
    if (payload.type == 'location') {
        url = url + "locations/" + payload.location_id + "/" + payload.location_name + "/?__a=1";
    } else if (payload.type == 'hashtag') {
        url = url + "tags/" + payload.hashtag + "/?__a=1";
    } else { //profile
        url = "https://www.instagram.com/" + payload.user_name + "/?__a=1";
    }

    request(url, function (error, response, body) {
        body = JSON.parse(body);
        //below are params which are required for load more pagination payload
        paginationData = {
            has_next_page: body.data.user.edge_owner_to_timeline_media.page_info.has_next_page,
            end_cursor: body.data.user.edge_owner_to_timeline_media.page_info.end_cursor
        };

        //user.edge_owner_to_timeline_media for profile posts,
        //hashtag.edge_hashtag_to_media for hashtag posts
        //location.edge_location_to_media for location posts
    });

and for load more items, I am using:

    let url = "https://www.instagram.com/graphql/query/";
    if (payload.type == 'location') {
        let variables = encodeURIComponent('{"id":"' + payload.pagination.id + '","first":50,"after":"' + payload.pagination.end_cursor + '"}');
        url = url + "?query_hash=ac38b90f0f3981c42092016a37c59bf7&query_id=17865274345132052&variables=" + variables;
    } else if (payload.type == 'hashtag') {
        let variables = encodeURIComponent('{"tag_name":"' + payload.pagination.tag_name + '","first":50,"after":"' + payload.pagination.end_cursor + '"}');
        url = url + "?query_hash=298b92c8d7cad703f7565aa892ede943&query_id=17875800862117404&variables=" + variables;
    } else { //profile
        let variables = encodeURIComponent('{"id":"' + payload.pagination.owner_id + '","first":50,"after":"' + payload.pagination.end_cursor + '"}');
        url = url + "?query_hash=472f257a40c653c64c666ce877d59d2b&query_id=17888483320059182&variables=" + variables;
    }

    let options = {
        url: url,
        headers: {
            Cookie: "Cookie value which i copied from my logged in instagram browser window"
        }
    };

    request(options, function (error, response, body) { });

It seems query_id is no longer required and query_hash is sufficient now. I'm not sure though but it seems working without them too for me.

Solution 7:[7]

As of 21 May 2021, using a /channel will make it work, but only if using a browser User-Agent header with your request, for example with a curl:

curl -H "User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36" https://www.instagram.com/{username}/channel/?__a=1

Solution 8:[8]

As of the current date 12 April 2018, 4:00PM (GMT+1), API queries work without any cookie. I have no idea what they're doing...

Just try this link in private navigation.

Solution 9:[9]

I faced a similar problem in that I was unable to parse the JSON file using "?__a=1" and ended up with JSONDecodeError: Expecting value. Searched in many places and finally found a catch, using Header solved the probelm. Try using this, it worked for me

link = 'http://instagram.com/instagram/?__a=1'

headers = {'User-Agent': 'Mozilla'}
r = requests.get(link, headers=headers)
data = r.json()

Solution 10:[10]

100% working as of now It can be circumvented using the session ID.

Solution 11:[11]

actually the position and tag changed if you look clearly we dont require any channel or anything url change at all the data is present under video versions attribute with many video quality actually enter image description here

but sometimes ?_a=1 working normally i.e you can see the short code in the start

Solution 12:[12]

It still works if you use residential proxies, for example via https://webscraping.ai/ API (note that the url parameter should be URL-encoded):

$ curl https://api.webscraping.ai/html?proxy=residential&api_key=test-api-key&url=https%3A%2F%2Fwww.instagram.com%2Fapple%2F%3F__a%3D1

{"seo_category_infos":[["Beauty","beauty"],["Dance & Performance","dance_and_performance"],["Fitness","fitness"],["Food & Drink","food_and_drink"],["Home & Garden","home_and_garden"],["Music","music"],["Visual Arts","visual_arts"]],"logging_page_id":"profilePage_5821462185","show_suggested_profiles":false,"graphql":{"user":{"biography":"Everyone has a story to tell. \nTag #ShotoniPhone to take part.","blocked_by_viewer":false,...