Instagram oAuth2无法获得令牌

时间:2017-02-18 18:15:58

标签: php curl instagram

我尝试为Instagram制作PHP脚本。

首先我请求“CODE”,然后通过CODE我请求一个TOKEN。使用令牌我执行端点。

当我通过命令行使用它时:

( https://www.instagram.com/developer/authentication/ )
    curl -F 'client_id=CLIENT_ID' \
    -F 'client_secret=CLIENT_SECRET' \
    -F 'grant_type=authorization_code' \
    -F 'redirect_uri=AUTHORIZATION_REDIRECT_URI' \
    -F 'code=CODE' \

...它完美无缺:我收到了TOKEN,但当我将它转换为CURL / PHP时,它失败了。

https://api.instagram.com/oauth/access_token?client_id=CLIENT_ID&grant_type=authorization_code&redirect_uri=http://www..rightu.uri....&code=12345677

当我通过我的PHP脚本运行它来获取令牌时,我得到一个空的Instagram输出页面。

我做错了什么?

$clientid = 'xxxxx';
$clientsecret = 'yyyyy';
$redirecturi    = 'http://www.krnet.url/instaread';

if($_GET["code"]){
    $code=$_GET["code"];
    echo "code = $code<br>";
    getAccessToken($accessToken,$code,$clientid,$clientsecret,$redirecturi);
}else{
    echo "click for token";
}

function getAccessToken($accessToken,$code,$clientid,$clientsecret,$redirecturi) {
    $url = "https://api.instagram.com/oauth/access_token?client_id={$clientid}&client_secret={$clientsecret}&grant_type=authorization_code&redirect_uri={$redirecturi}&code={$code}";
    echo "<a href=$url>$url</>";
}

if (isset($zaccessToken)) {
    $url = "https://api.instagram.com/v1/users/self/follows?access_token={$accessToken}";
    $headers = array("Content-type: application/json");

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_COOKIEJAR,'cookie.txt');
    curl_setopt($ch, CURLOPT_COOKIEFILE,'cookie.txt');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3");
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

    $st=curl_exec($ch);
    foreach ($result['data'] as $item) {

    }
} else {
    $loginUrl = 'https://api.instagram.com/oauth/authorize/?client_id=1d50f5039bfc4d57aa845a978da25169&redirect_uri=http://www.kraan.net/instaread&response_type=code';
    echo '<center><a href="' . $loginUrl . '"><img src=instagram.png></a></center>';
}

0 个答案:

没有答案