Instagram 如何从用户名中获取我的用户 ID?

我正在使用 JSON 将我的图像提要嵌入到我的网站中,URL 需要我的用户 ID,所以我可以检索这个提要。

那么,我在哪里可以找到/获得我的用户 ID?

392973 次浏览

您需要使用 Instagrams API将您的 username转换为 id

如果我没记错的话,您使用 users/search查找用户名并从中获取 ID

要获取您的 id,请向 Instagram API users/self/feed端点发出经过身份验证的请求。除其他数据外,响应将包含用户名和用户 ID。

虽然它不再列在 API 文档页面上,但是我发现了一个 线,它提到您可以使用 self代替 user-id作为 users/{user-id}端点,它将返回当前经过身份验证的用户的信息。

因此,users/self与对 users/{some-user-id}的显式调用相同,并且包含用户的 id 作为有效负载的一部分。一旦进行了身份验证,只需调用 users/self,结果就会包含当前经过身份验证的用户的 ID,如下所示:

{
"data": {
"id": "1574083",
"username": "snoopdogg",
"full_name": "Snoop Dogg",
"profile_picture": "http://distillery.s3.amazonaws.com/profiles/profile_1574083_75sq_1295469061.jpg",
"bio": "This is my bio",
"website": "http://snoopdogg.com",
"counts": {
"media": 1320,
"follows": 420,
"followed_by": 3410
}
}

在浏览器中输入此 URL,并显示要查找的用户名和访问标记

https://api.instagram.com/v1/users/search?q=[USERNAME]&access_token=[ACCESS TOKEN]

我编写了这个工具,通过用户名 Instagram 用户 ID 查找检索 Instagram ID。

它利用 Python-instagram库访问 API,并包含一个 链接到源代码(在 Django 上编写) ,它演示了 Instagram API 的各种实现。

更新 : 增加了 端口到 Ruby on Rails的源代码。

进入 api 控制台 & copy link https://api.instagram.com/v1/users/self in text field 并使用 instagram id & password 进行身份验证,您将获得响应的 id

您可以使用 Instagram API(用户端点:/用户/搜索)来完成此操作

返回文章页面如何使用 php:

function Request($url) {


$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);


$result = curl_exec($ch);


curl_close($ch);


return $result;


}


function GetUserID($username, $access_token) {


$url = "https://api.instagram.com/v1/users/search?q=" . $username . "&access_token=" . $access_token;


if($result = json_decode(Request($url), true)) {


return $result['data'][0]['id'];


}


}


// example:
echo GetUserID('rathienth', $access_token);

这可以通过 apigee.com Instagram API 访问 Instagram 开发者站点上的 给你来完成。登录后,单击“/users/search”API 调用。从那里您可以搜索任何用户名并检索其 ID。

{
"data": [{
"username": "jack",
"first_name": "Jack",
"profile_picture": "http://distillery.s3.amazonaws.com/profiles/profile_66_75sq.jpg",
"id": "66",
"last_name": "Dorsey"
},
{
"username": "sammyjack",
"first_name": "Sammy",
"profile_picture": "http://distillery.s3.amazonaws.com/profiles/profile_29648_75sq_1294520029.jpg",
"id": "29648",
"last_name": "Jack"
},
{
"username": "jacktiddy",
"first_name": "Jack",
"profile_picture": "http://distillery.s3.amazonaws.com/profiles/profile_13096_75sq_1286441317.jpg",
"id": "13096",
"last_name": "Tiddy"
}]}


如果你已经有一个访问代码,它也可以这样做: Https://api.instagram.com/v1/users/search?q=username&access_token=access_token

轻松获取用户 ID 和用户详细信息

Https://api.instagram.com/v1/users/search?q= [ USER NAME ] & Client _ ID = [ YOAPP 客户端 ID ]

例如:

Https://api.instagram.com/v1/users/search?q=zeeshanakhter2009&client_id=enter_your_id

结果:

{“ meta”: {“ code”: 200} ,“ data”: [{“ username”: “ zeeshanakhter2009”,“ bio”: “ http://about.me/zeeshanakhter","website":"http://zeeshanakhter.com","profile_picture":"http://images.ak.instagram.com/profiles/profile_202090411_75sq_1377878261.jpg","full_name":"zeeshan 阿克特,“ id”: “202090411”]}

可以检查 Instagram 用户页面来获取 id,而不是使用 API:

$html = file_get_contents("http://instagram.com/<username>");
$doc = new DOMDocument();
$doc->loadHTML($html);


$xpath = new DOMXPath($doc);
$js = $xpath->query('//body/script[@type="text/javascript"]')->item(1)->nodeValue;


$start = strpos($js, '{');
$end = strrpos($js, ';');
$json = substr($js, $start, $end - $start);


$data = json_decode($json, true);
$data = $data["entry_data"]["UserProfile"][0];
# The "userMedia" entry of $data now has the same structure as the "data" field
# in Instagram API responses to user endpoints queries


echo $data["user"]["id"];

当然,如果 Instagram 改变了页面格式,这段代码也必须进行调整。

如果您使用的是隐式身份验证,那么一定存在无法找到 < kbd > user _ id 的问题

我发现了一种方法,例如:

Access Token = 1506417331.18 b98f6.8 a00c0d293624ded801d5c723a25d3ec 访问令牌 = 1506417331.18 b98f6.8 a00c0d293624ded801d5c723a25d3ec 用户 ID 是1506417331

是否执行以. 分隔的拆分单元,以访问标记和第一个元素

目前还没有直接的 Instagram API 来从用户名中获取用户 ID。您需要调用 GET/users/search API,然后迭代结果并检查 username字段值是否等于您的用户名,然后获取 id

首先在 Instagram 上创建一个应用程序,并为您的应用程序获取客户端 ID

Http://instagram.com/developer/

现在只需通过替换用户名和客户 ID 将 Url 后面的粘贴复制到浏览器窗口中 Https://api.instagram.com/v1/users/search?q= [ Your-username ] & client _ id = [ your-client-Id ]

您将获得一个 Json 结果,其中包含有关您的帐户的一般信息以及您的数字用户 ID

我认为最好、最简单、最安全的方法是在浏览器中使用 打开你的 Instagram 档案,在主 javascript 代码中使用 查看源代码查看源代码查找用户变量(ctrl + f 「使用者」 : {)。用户变量中的 id 号应该是您的 id。

下面是它在写这个答案的时候看起来的样子(它可以,而且很可能在将来会改变) :

"user":{"username":"...","profile_picture":"...","id":"..........","full_name":"..."}},

下面是如何从用户名中检索用户 ID:

$url = "https://api.instagram.com/v1/users/search?q=[username]&access_token=[your_token]";
$obj = json_decode(@file_get_contents($url));
echo $obj->data[0]->id;
https://api.instagram.com/v1/users/search?q="[USERNAME]"&access_token=[ACCESS TOKEN]

请注意引号。

这并不总是返回有效的结果,但往往比未引用的结果更有效:

https://api.instagram.com/v1/users/search?q="self"&count=1&access_token=[ACCESS TOKEN]

返回用户“ self”(id: 311176867)

https://api.instagram.com/v1/users/search?q=self&count=1&access_token=[ACCESS TOKEN]

返回用户“ super _ selfie”(id: 1422944651)

这些答案大多数在 6/1/2016 Instagram API 更改之后是无效的。现在最好的解决方案是 给你。转到 instagram.com 的 feed,复制任何图片的链接地址,粘贴到该页面的文本框中。非常有效。

大多数方法自2016年6月1日 API 更改以来已经过时

下面是我的工作,

  • 在你的浏览器上访问 instagram,比如 chrome,safari 或者 firefox。
  • 启动开发工具,进入控制台选项。
  • 在命令提示符下输入以下命令,然后按回车键:

    window._sharedData.entry_data.ProfilePage[0].user.id
    

If you are lucky, you will get at first attempt, if not, be patient, refresh the page and try again. keep doing until you see user-id. Good luck!!

2022年6月5日更新,Instagram API 不再使用承载令牌进行身份验证。但我找到了另一个有用的 API。所有您需要的是添加额外的标题 X-IG-App-ID 与“神奇的价值”。

https://i.instagram.com/api/v1/users/web_profile_info/?username=therock

可以使用我的 Docker 容器 Insta-Proxy-Server 绕过身份验证。

https://hub.docker.com/repository/docker/dockerer123456/insta-proxy-server

演示视频(我只是直接从源代码运行) : https://www.youtube.com/watch?v=frHC1jOfK1k


在2022年3月19日更新,API 现在需要登录。对不起这个坏消息。

但是我们可以用两种方法来解决这个问题。

  1. 使用 我的 C # lib,使用您的帐户登录(没有任何 Instagram 应用程序令牌和图形 API 的东西。)
  2. 如果 lib 失败了(我很久以前就不再维护它了) ,创建一个登录了 instagram 帐户的代理服务器。

[你的应用]—— > [代理服务器]—— > [ Instagram ]—— > [代理服务器]-(前进)-> [你的应用]

对于代理服务器,您可以使用 Nodejs 应用程序安装 Chromium headless 模块(例如 Puppeteer) ,用 Instagram 帐户登录。

概念证明:

Https://www.youtube.com/watch?v=zlnnbpcxqm8

Https://www.youtube.com/watch?v=emb9us2hh3w


在2019年6月20日更新,API 现在是公开的,不需要认证。


更新于2018年12月11日,我需要确认这个端点仍然工作。

在向这个站点发送请求之前,您需要登录,因为它不再是公共端点了。


更新于2018年4月17日,看起来这个端点仍在工作(但不再是公共端点了) ,您必须向该端点发送带有额外信息的请求。(按 F12打开开发人员工具栏,然后单击“网络选项卡”并跟踪请求。)


更新于2018年4月12日,cameronjonesweb 说这个端点不再工作了。当他/她试图访问这个端点时,403状态码返回。


你可以通过下面的网址获得用户信息:

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

例如:

这个 URL 将获得用户名为 therock 的用户的所有信息

https://www.instagram.com/therock/?__a=1

这是一个非常简单的网站,对我来说非常有用:

Http://www.instaid.co.uk/

或者你可以用你的 Instagram账号代替“用户名”

Https://www.instagram.com/username/?__a=1

或者你可以登录到你的 Instagram 账户,使用谷歌开发工具,查看已经存储的 cookie。“ ds _ user _ ID”是您的用户 ID

enter image description here

我尝试了所有上述的解决方案,但没有一个奏效。我猜 Instagram 加速了他们的变化。然而,我尝试了浏览器控制台方法,并且尝试了一下,发现了这个命令,它给了我用户 ID。

window._sharedData.entry_data.ProfilePage[0].graphql.user.id

您只需访问配置文件的页面并在控制台中输入这个命令。不过,您可能需要刷新页面才能正常工作。(因为我的名声不好,我不得不把这个作为答案贴出来)

工作解决方案 ~ 2018

我发现,如果你有一个访问令牌,你可以在你的浏览器中执行以下请求:

https://api.instagram.com/v1/users/self?access_token=[VALUE]

事实上,访问令牌包含 用户名(令牌的第一个段) :

<user-id>.1677aaa.aaa042540a2345d29d11110545e2499

可以使用 这个工具由像素联盟提供获取访问令牌。

截至2018年10月14日的工作解决方案 没有访问令牌:

搜索用户名:

https://www.instagram.com/web/search/topsearch/?query=<username>

例如:

https://www.instagram.com/web/search/topsearch/?query=therock

这是一个搜索查询。在回复中找到完全匹配的条目并从条目中获得用户 ID。

工作解决方案2020年12月14日

对于需要 Instagram 用户 ID 的第三方工具(比如嵌入图像提要)这样的简单用法,我倾向于使用:

Https://www.thekeygram.com/find-instagram-user-id/

因为它使得复制和粘贴我正在寻找的 Instagram 用户 ID 变得非常容易。不像大多数工具,我得到的结果很快,它是免费的,没有广告。我建议你在使用之前先看看 youtube 视频,这样你就可以看到它是多么的简单,并了解它是如何使用的:

Https://www.youtube.com/watch?v=9hvoroy-ybw

对于更高级的用法,我推荐:

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

(用请求的用户名替换用户名)

例如,要查找您将使用的用户名“ instagram”的用户 ID:

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

这是返回 JSON 响应的最高级方法,如果您正在构建一个需要原始数据的应用程序,那么这种方法非常好。您可以将它保存在数据库中,或者构建某种类型的前端 UI 来显示它。例如: 在仪表板或网站上。此外,使用网址是伟大的,因为你可以得到额外的属性关于用户,如他们的关注者总数和个人资料简介。

更新于2021年

只要去 Facebook 应用程序选择你的应用程序连接到 Instagram,你会看到你的 Instagram ID: * * * * * * * * * *

enter image description here

注意 https://www.instagram.com/{username}/?__a=1对我来说不起作用,所以如果你想使用 Instagram Graph API,这不是2021年的解决方案

从2022年6月开始,您可以运行或拦截一个特殊的 HTTP 请求,以便成功获取用户数据(和用户 ID)。如果您使用 Puppeteer,则可以拦截 Instagram 在浏览器中发出的请求,并读取其响应。示例代码:

const username = 'user.account';


const page = await browser.newPage();


const [foundResponse] = await Promise.all([
page.waitForResponse((response) => {
const request = response.request();
return request.method() === 'GET' && new RegExp(`https:\\/\\/i\\.instagram\\.com\\/api\\/v1\\/users\\/web_profile_info\\/\\?username=${encodeURIComponent(username.toLowerCase())}`).test(request.url());
}),
page.goto(`https://instagram.com/${encodeURIComponent(username)}`),
]);


const json = JSON.parse(await foundResponse.text());
console.log(json.data.user);

参见讨论: https://github.com/mifi/SimpleInstaBot/issues/125#issuecomment-1145354294

请参阅此处的工作代码: https://github.com/mifi/instauto/blob/2de64d9a30dad16c89a8c45f792e10f137a8e6cb/src/index.js#L250

因为将 ?__a=1添加到配置文件 URL 中不再能从用户名中获得用户 ID,所以我们可以使用 cURL 和 jq (新的 API 端点可以在 Instagram web 版本的网络请求中找到,例如使用 Firefox 开发工具) :

curl -s 'https://i.instagram.com/api/v1/users/web_profile_info/?username=alanarblanchard' -H 'X-IG-App-ID: 936619743392459' | jq -r .data.user.id

如果你在浏览器中使用 Instagram,你不需要使用上面的命令,可以直接检查 HTTP 请求的响应。

您可能还有兴趣从用户 ID 中查找用户名,以防有人频繁更改用户名。我在这里加了一个答案: Instagram 从 userId 获取用户名

使用 安装程序外部库的 Python 解决方案(首先使用 pip安装它)

import instaloader


YOUR_USERNAME = "Your username here"
USERNAME_OF_INTEREST = "Username of interest here"


L = instaloader.Instaloader()
L.interactive_login(YOUR_USERNAME)
profile = instaloader.Profile.from_username(L.context, USERNAME_OF_INTEREST)
print(profile.userid)

对于这类关于不断更改私有 API 的问题,我建议依靠积极地开发库,而不是依靠服务或答案。