【发布时间】:2020-01-30 10:54:07
【问题描述】:
主要目标:获取 access_token,以便我可以在网站上显示我的 Instagram 供稿。
目前,要获取访问令牌,我们去这个url点击授权获取代码,进一步交换access_token。
https://api.instagram.com/oauth/authorize
?client_id={app-id}
&redirect_uri={redirect-uri}
&scope=user_profile,user_media
&response_type=code
现在,如果我正在编写一个 PHP-curl 脚本来发送此 access_token 并获取我的帖子,我将如何每次获取访问令牌,我无法在每次我的 API 请求数据时真正点击授权,进一步,点击每 60 天授权一次对我来说也不是一个长期的解决方案。
所以我希望有办法(我可以调用此 URL,并直接获取授权码?)
到目前为止我的脚本:
$authURL = "https://api.instagram.com/oauth/authorize
?client_id=$client_id
&redirect_uri=$redirect_uri
&scope=user_profile,user_media
&response_type=code";
//STEP 1, GET THE AUTHORIZATION CODE (i am stuck here, how to get code from only PHP,
//without external clicks..)
//https://www.redirecturl.com?code=AQgw#_
$authorization_code = '48FduaX0g.....VZIj';
//STEP 2 GIVE THE CODE FOR TOKEN
$url = 'https://api.instagram.com/oauth/access_token';
$myjson = "
{'client_id': $client_id,
'client_secret': $client_secret,
'grant_type':'authorization_code',
'redirect_uri':$redirect_uri,
'code': $authorization_code
}";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $myjson);
$result = curl_exec($ch);
curl_close($ch);
$insta_details = json_decode($result);
echo $insta_details['access_token'];
curl -X GET \
'https://graph.instagram.com/17841405793187218/media?access_token=IGQVJ...'
【问题讨论】:
标签: php rest curl instagram instagram-graph-api