Instagram 新 API - 如何使用纯 PHP 代码获取授权码
Instagram New API - how to get authorization code using purely PHP code
主要目标: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.
来做到这一点
但是如果您只需要在使用 PHP 的网站上显示提要,您可以使用这个包:https://packagist.org/packages/espresso-dev/instagram-basic-display-php
在auth后的回调中抓取code
:
$code = $_GET['code'];
然后使用以下方法:
getOAuthToken()
获取初始代币
getLongLivedToken()
获取有效期为60天的long-lived token
存储长期令牌,以便可用于检索帖子,只需每 50 天左右调用一次 refreshToken()
方法即可在后台刷新令牌并更新您正在使用的令牌.
主要目标: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.
来做到这一点但是如果您只需要在使用 PHP 的网站上显示提要,您可以使用这个包:https://packagist.org/packages/espresso-dev/instagram-basic-display-php
在auth后的回调中抓取code
:
$code = $_GET['code'];
然后使用以下方法:
getOAuthToken()
获取初始代币getLongLivedToken()
获取有效期为60天的long-lived token
存储长期令牌,以便可用于检索帖子,只需每 50 天左右调用一次 refreshToken()
方法即可在后台刷新令牌并更新您正在使用的令牌.