我正在尝试从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"
}
这时我很困,不胜感激。
答案 0 :(得分:1)
在此行中将“ $ cod”更改为“ $ code”:
public function accesstoken($app_id, $redirect_uri, $app_secret, $cod)
和:
$curl_post = 'app_id='.$app_id. '&redirect_uri='.$redirect_uri.'&app_secret='.$app_secret.'&code='.$cod.'grant_type=authorization_code';