【发布时间】:2019-11-08 08:04:42
【问题描述】:
我正在尝试从 Instagram Basic Display API 访问 ACCESS_TOKEN。我成功地获得了下面提到的代码,现在我试图将这个$_GET['code'] 传递给accesstoken(),但没有得到响应(错误)。
//sample request demo (string(240))
https://www. mysite.org/dashboard.php?code=AQAWTCkmCLVYUJddAqjcNvhh_BZDJg-68vSK1bun3KdNp3nbLdcjexCncu_LvPtk4jY5bJTCXe4vJ9yldmBsUZzE0heDtkhhd--SrPlCer0Lq5J25qZ_X9OBQ5AokmxCum4kz6kgqN1ilq6ZLT1m84mIJ0_hhLVKXwaPTprUXgRtmm1Gat5NbdbhtuXjOqMgD9yFfe94QVsV-aQ7CwKpAtPTrT9_nSUVDVedF0JhbqvWbQ#_
在下面我刚刚删除了$_GET['code'] 末尾的#_ 并将其传递给函数。
$object = new instaAPI();
$code = trim($_GET['code'], "#_"); //removed the tralling #_ from the end.
$access = $object->accesstoken(APP_ID, REDIRECT_URI, APP_SECRET, $code); //calling
echo $access['access_token']; // blank
卷曲函数
class instaAPI
{
public function accesstoken($app_id, $redirect_uri, $app_secret, $cod)
{
$url = 'https://api.instagram.com/oauth/access_token';
$curl_post = 'app_id='.$app_id. '&redirect_uri='.$redirect_uri.'&app_secret='.$app_secret.'&code='.$cod.'grant_type=authorization_code';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $curl_post);
$data = json_decode(curl_exec($ch), true);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if($http_code !== 200)
throw new Exception('Error : Failed to receieve access token'.'IG_ERROR_TYPE:'.$data['error_type'].'CODE:'.$data['code'].'MESSAGE'.$data['error_message']);
return $data;
}
}
响应或 Json 响应
Error: Failed to receive access token
IG_ERROR_TYPE: OAuthException
CODE:400
MESSAGE: Matching code was not found or was already used
或
{
"error_type": "OAuthException",
"code": 400,
"error_message": "Matching code was not found or was already used"
}
我被困在这一点上,感谢任何帮助。
【问题讨论】:
-
您应该首先对 API 请求中的参数值应用适当的 URL 编码。
-
$code = trim($_GET['code'], "#_"); //removed the tralling #_ from the end.- 当然绝对没有必要,$_GET['code']的末尾从来没有#_ -
$_GET['code'] 输出为www。 mysite.org/dashboard.php?code=AQAWTCkmCLVYUJddAqjcNvhh_BZDJg-68vSK1bun3KdNp3nbLdcjexCncu_LvPtk4jY5bJTCXe4vJ9yldmBsUZzE0heDtkhhd--SrPlCer0Lq5J25qZ_X9OBQ5AokmxCum4kz6kgqN1ilq6ZLT1m84mIJ0_hhLVKXwaPTprUXgRtmm1Gat5NbdbhtuXjOqMgD9yFfe94QVsV-aQ7CwKpAtPTrT9_nSUVDVedF0JhbqvWbQ #_ 跨度>
-
见 ....WbQ#_
-
#_是 URL 的一部分,但它不是参数code值的一部分。 (这就是 URL 工作的方式 -#开始 fragment 部分。)
标签: php api token instagram instagram-api