【发布时间】:2021-03-12 10:39:28
【问题描述】:
我需要允许我平台的用户通过 Instagram 基本显示 API 链接他们的 Instagram 帐户。 当我调用获取代码以交换访问令牌时,它始终相同,并导致下一步出错。
这些是我正在执行的步骤,遵循this official guide:
- 带有这些参数的GET请求:
https://api.instagram.com/oauth/authorize?client_id=XXXXX&redirect_uri=XXXXX&scope=user_profile&response_type=code - 通过 PHP 中的 cURL 向
https://api.instagram.com/oauth/access_token发送 POST 请求,将参数作为下面的第二个代码 sn-p 传递 - 通过 PHP 中的 cURL 使用
'https://graph.instagram.com/' . $result->user_id . '?fields=username&access_token=' . $result->access_token获取请求
该过程在 1 和 2 之间失败,因为即使 我已经用不同的帐户进行了多次尝试
,第一步总是返回相同的代码这是代码请求的 HTML 表单。成功打开 Instagram 登录页面:
<a class="insta-con" href="https://api.instagram.com/oauth/authorize?client_id=XXXXX&redirect_uri=XXXXX&scope=user_profile&response_type=code">
<img src="assets/images/instalogo.png" alt="Connetti a Instagram">
<p>Connect to Instagram</p>
</a>
登录表单总是返回相同的代码,也使用不同的帐户,导致在下一个请求中出现错误{"error_type": "OAuthException", "code": 400, "error_message": "This authorization code has been used"}:
$fields = array(
'client_id' => 'XXXXXX',
'client_secret' => 'XXXXXXXXX',
'grant_type' => 'authorization_code',
'redirect_uri' => 'https://www.XXXXXXX.it/connect.php',
'code' => $_GET['code']
);
$url = 'https://api.instagram.com/oauth/access_token';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
curl_setopt($ch, CURLOPT_POST,true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_VERBOSE,true);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0');
$result = curl_exec($ch);
if(curl_errno($ch)) {
echo 'Curl error: ' . curl_error($ch);
}
curl_close($ch);
$result = json_decode($result, true);
所以我的问题是:尽管帐户不同,为什么我总是得到相同的代码?
【问题讨论】:
-
你为什么用一个表单来触发这个,而不是一个普通的链接?顺便说一句,您的表单是无效的 HTML,您不能将
input嵌套到button。 -
(你现在应该先去重置你的客户端密码,因为你刚刚在上面的代码中暴露了它。)
-
我的错误。感谢您的建议,但问题仍然存在
-
我不知道你为什么每次都会得到相同的代码,这没什么意义。你能解释一下你在这里执行的确切步骤吗?即先登录Facebook/IG,然后调用登录对话框等;以及在每个步骤中究竟发生了什么?您是切换了登录的 IG 用户,还是仍在为同一个帐户尝试此操作?
-
我已经更详细地编辑了这个问题,解释了我采取的步骤。尝试使用不同帐户时也会出现此问题