Javascript Instagram ?__a=1 url 不再工作 & 使用 graphql/query 获取数据的问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49265339/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 04:20:48  来源:igfitidea点击:

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

javascriptinstagraminstagram-api

提问by Pablo

Update 19 April

4 月 19 日更新

After a few days using cookie ig_prtwo days ago is block. Looks like the only way to get the data now is use sessionidwith a specific value

两天前使用 cookieig_pr几天后被阻止。看起来现在获取数据的唯一方法是使用sessionid特定值

Original

原来的

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

我正在使用 instagram ?__a=1 url 来阅读 instagram 用户的所有帖子。

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

几个小时前,响应发生了变化,现在不允许我使用max_id分页。

Before I usually sent a request to

在我通常发送请求之前

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

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

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

graphql.edge_owner_to_timeline_media.page_info.end_cursor在响应中使用我用新的 max_id 调用了同一个页面

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

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

Now the end_cursorchanges in each call & max_id is not working.

现在end_cursor每次调用和 max_id 中的更改不起作用。

Please help :)

请帮忙 :)

采纳答案by Felix Ng

The query_hash does not change, at least in the past few days. It indicate what TYPE of query it is.

query_hash 没有改变,至少在过去几天中如此。它表明它是什么类型的查询。

Below listed 4 query types I knew, hope these help.

下面列出了我知道的 4 种查询类型,希望这些有帮助。

Load more mediaunder https://www.instagram.com/someone/?__a=1

https://www.instagram.com/someone/?__a=1下加载更多媒体

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

(Instagram blocked the above access since 2018-04-12. You have to remove the __a=1 and extract the JSON inside a block. Look for "window._sharedData" in the HTML)

(Instagram 自 2018-04-12 起阻止了上述访问。您必须删除 __a=1 并提取块内的 JSON。在 HTML 中查找“window._sharedData”)

Load more mediaunder https://www.instagram.com/explore/tags/iphone/?__a=1

https://www.instagram.com/explore/tags/iphone/?__a=1下加载更多媒体

https://www.instagram.com/graphql/query/?query_hash=298b92c8d7cad703f7565aa892ede943&variables={"tag_name":"iphone","first":12,"after":"XXXXXXXX"}

Load more mediaunder https://www.instagram.com/explore/locations/703629436462521/?__a=1

https://www.instagram.com/explore/locations/703629436462521/?__a=1下加载更多媒体

https://www.instagram.com/graphql/query/?query_hash=ac38b90f0f3981c42092016a37c59bf7&variables={"id":"703629436462521","first":12,"after":"XXXXXXXX"}

Load more commentsfor https://www.instagram.com/p/Bf-I2P6grhd/

https://www.instagram.com/p/Bf-I2P6grhd/加载更多评论

https://www.instagram.com/graphql/query/?query_hash=33ba35852cb50da46f5b5e889df7d159&variables={"shortcode":"Bf-I2P6grhd","first":20,"after":"XXXXXXXX"}

where XXXXXXXX is the end_cursor from the original request

其中 XXXXXXXX 是原始请求中的 end_cursor

回答by Daniel Choi

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.

看起来他们只是通过删除max_id 来更改他们的私有 api 。他们的网站似乎已经用 graphql api 替换了旧的 api。

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

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']
  • id: 用户 ID,
  • 第一:要获得的节点数量,
  • 之后:您可以从 data['user']['edge_owner_to_timeline_media']['page_info']['end_cursor'] 中获得的“end_cursor”

use either query_hash or query_id

使用 query_hash 或 query_id

  • query_hash: 472f257a40c653c64c666ce877d59d2b
  • query_id: 17888483320059182
  • query_hash:472f257a40c653c64c666ce877d59d2b
  • 查询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.

我不知道 query_id/query_hash 能用多久,这取决于 Instagram。他们最终会改变它。

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

2018年 4 月 8 日更新- 在 FB 没有检查任何 cookie 之前,但看起来他们添加了快速验证。ig_pr=2在发送您的 API 时尝试添加到请求 cookie。这个快速修复对我有用。谁知道FB什么时候又换了。

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

只要 FB 不对这些基本 API 强制登录,总会有一个简单的解决方法。

回答by Pablo

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

使用图形/查询的主要问题是我只有用户名,要提取 userId 和最后一个帖子,就像我们以前用 ?__a=1 做的那样,我们必须获取 instagram 的用户页面并提取 _sharedData

Javascript

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)

获得所有这些数据后,我们可以调用图形/查询(不在客户端)

回答by Zerquix18

Translated some of the folks' code to PHP:

将一些人的代码翻译成 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

不知道能用多久。对于我的小项目,它现在可以完成工作。结果与 URL 中的结果非常相似(如果不相等):instagram.com/{user}/?__a=1

回答by Noushad

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 Cookieheader set. Below are some codes for fetching Instagram public APIS

这个答案并没有直接帮助问题,而是发布,因为有人可能会从答案中受益。截至当前日期 2018 年 4 月 12 日,如果没有Cookie标头集,加载更多 API 将无法运行。下面是一些获取 Instagram 公共 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_idis no longer required and query_hashis sufficient now. I'm not sure though but it seems working without them too for me.

似乎query_id不再需要,query_hash现在就足够了。我不确定,但对我来说似乎没有它们也能工作。

回答by Igor Sovcik

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

对于分页,您现在可以使用 ?__a=1&page=2

回答by Pierre Grimaud

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...

截至当前日期 2018 年 4 月 12 日下午 4:00 (GMT+1),API 查询无需任何 cookie。我不知道他们在做什么...

Just try this linkin private navigation.

只需在私人导航中尝试此链接即可