[英]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.