简体   繁体   English

为什么 Instagram API 会为同一个用户返回两个不同的 user_id 值?

[英]Why does Instagram API return two different user_id values for the same user?

Nutshell: PHP implementation gets correct user_id, React.js implementation gets incorrect user_id.简而言之:PHP 实现得到正确的 user_id,React.js 实现得到不正确的 user_id。 Why??为什么??

I implemented the Instagram Basic Display API OAuth flow in PHP and in Javascript under React.我在 PHP 和 Z9E13B69D1D2DA937102ACAAAF7Z5 下的 PHP 中实现了 Instagram 基本显示 API OAuth 流。

The flow under PHP works fine - I get a code, I POST it using curl to /oauth/access_token , I receive a client_id and an access_token , all fine, subsequent Graph API requests work just fine. PHP 下的流程工作正常 - 我得到一个代码,我使用POST将其发布到/oauth/access_token ,我收到一个client_id和一个access_token ,一切都很好,随后的图形 ZDB974AZ238718CA8DEF4 请求工作正常。

Then I moved to rebuild it in Javascript.然后我开始在 Javascript 中重建它。 Here, when I get a code, and I POST it using fetch to /oauth/access_token , with the same client_id and client_secret , and the same authenticating user, essentially the same request, just a different redirect URL (obviously), I receive a token and a different user_id , one that does not work (it gets a 400 error from Graph API).在这里,当我得到一个代码时,我使用POST将其fetch/oauth/access_token ,具有相同的 client_id 和 client_secret ,以及相同的身份验证用户,基本上相同的请求,只是不同的重定向 URL (显然),我收到一个token 和一个不同的 user_id ,一个不起作用(它从 Graph API 收到 400 错误)。

Oddly enough, the bad user_id is consistently smaller by 1 than the one that I get with PHP: the good one ends with 93, the bad one ends with 92. If I do the unspeakable and just manually increase the wrong user_id I received, I obtain a valid user_id for which the token works!奇怪的是,坏的 user_id 总是比我用 PHP 得到的小 1:好的以 93 结尾,坏的以 92 结尾。获取令牌适用的有效 user_id!

So why is Instagram sending me a wrong user_id, and how can I help it?那么,为什么 Instagram 向我发送了错误的 user_id,我该如何解决呢?

I finally found the cause, moments ago, after writing the question and earlier wasting 5 hours on analyzing whatever I could think of.不久前,我终于找到了原因,在写完这个问题之后,我浪费了 5 个小时来分析我能想到的任何东西。

The user_id is sent as a number larger than Number.MAX_SAFE_INTEGER, and thus JSON.parse fails to parse it correctly . user_id作为大于 Number.MAX_SAFE_INTEGER 的数字发送,因此 JSON.parse 无法正确解析它 Instagram should be encoding the user_id field as a string. Instagram 应该将user_id字段编码为字符串。 This API endpoint thus needs to be parsed manually.因此需要手动解析此 API 端点。 I "fixed" mine by filtering the results as such: text = text.replace(/"user_id":\s*(\d+)/,'"user_id":"$1"') .我通过过滤结果来“修复”我的结果: text = text.replace(/"user_id":\s*(\d+)/,'"user_id":"$1"') Still, encoding such large numbers as "numbers" in JSON is a horrible practice and Instagram should fix their API instead.尽管如此,在 JSON 中将如此大的数字编码为“数字”是一种可怕的做法,Instagram 应该改为修复他们的 API。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用user_id的javascript对象过滤器不返回预期结果 - javascript object filter using user_id not return expect result 在两个 MySql 表之间共享 user_id 值 - Sharing user_id value between two MySql table 我如何抓取 HTML 复选框表单值并检查 ID 的值是否与 User_id 相同以自动增加投票 - How do i grab HTML Checkbox Form Values and check if the Value of the ID is the same as the User_id to auto increment Votes 通过user_id减少对象数组并求和某些值 - Reduce array of objects by user_id and sum certain values Node.js - SequelizeDatabaseError:列“user_id”不存在 - Node.js - SequelizeDatabaseError: column “user_id” does not exist 为 GroupMe API 中给定 user_id 的组确定用户的 members_id - Determining user's membership_id for a group given user_id in GroupMe API 为什么对同一函数的两个绑定返回不同的值 - Why two bindings to the same function return different values 为什么此javascript函数针对同一查询返回两个不同的结果? - Why does this javascript function return two different results for the same query? 通过USER_ID获取节点(查询) - Get nodes by USER_ID (query) 无法读取未定义的属性“user_id” - Cannot read property 'user_id' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM