【问题标题】:Instagram Get Access Token from Code in PHPInstagram 从 PHP 代码中获取访问令牌
【发布时间】:2017-08-05 17:58:36
【问题描述】:

以下 php 代码不适用于我获取访问令牌 在客户端 ID 和密钥中,我已替换为我的真实客户端 ID 和密钥。

<?php
$client_id = 'MY CLIENT ID';
$client_secret = 'MY CLIENT SECRET';
$redirect_uri = 'http://localhost/instagram/demo.php';
$scope = 'basic+likes+comments+relationships';

$url = "https://api.instagram.com/oauth/authorize?client_id=$client_id&redirect_uri=$redirect_uri&scope=$scope&response_type=code";

if(!isset($_GET['code']))
{
    echo '<a href="'.$url.'">Login With Instagram</a>';
}
else
{
    $code = $_GET['code'];

$apiData = array(
  'client_id'       => $client_id,
  'client_secret'   => $client_secret,
  'grant_type'      => 'authorization_code',
  'redirect_uri'    => $redirect_uri,
  'code'            => $code
);

$apiHost = 'https://api.instagram.com/oauth/access_token';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $apiHost);
curl_setopt($ch, CURLOPT_POST, count($apiData));
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($apiData));
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$jsonData = curl_exec($ch);
curl_close($ch);

var_dump($jsonData);
$user = @json_decode($jsonData); 

echo '<pre>';
print_r($user);
exit;
}
?>

上面的代码总是返回 false。我不知道为什么这不起作用。

有人调试这段代码并告诉我我的代码有什么问题吗?

【问题讨论】:

    标签: instagram


    【解决方案1】:

    因为它已经是 https 所以你需要把它放在你的代码中

    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);  
    curl_setopt($ch,CURLOPT_SSL_VERIFYHOST, false);
    

    这与您的重定向 uri 是否在本地无关。这是因为你的 curl 选项中没有设置 SSL_VERIFYPEER

    【讨论】:

      【解决方案2】:

      您的代码存在一些问题。一方面,您的 redirect_uri 似乎是 localhost。显然,Instagram 的 localhost 是他们自己的服务器。您需要提供一个 FQDN/world 可访问的 url。

      同样来自http://php.net/manual/en/function.curl-setopt.php的代码

      curl_setopt($ch, CURLOPT_POST, count($apiData));
      

      应该很简单

      curl_setopt($ch, CURLOPT_POST, 1);
      

      【讨论】:

        【解决方案3】:

        你的代码是正确的,但你需要改变

        $redirect_uri = 'your site addres (not localhost)';

        例子:

        $client_id        = 'YOUR_CLIENT_ID';
        $client_secret    = 'YOUR CLIENT SECRET';
        $redirect_uri     = 'http://www.YOUR_SERVER.com/instagram/demo.php';
        $scope            = 'basic+likes+comments+relationships';
        

        如果你执行这个脚本,你可以得到这个结果。

        stdClass Object
        (
            [access_token] => 'your_ID'.xxxxxxx.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
            [user] => stdClass Object
                (
                    [username] => 'your username'
                    [bio] => 
                    [website] => 
                    [profile_picture] => 'your url picture profile' 
                    [full_name] => 
                    [id] => 'your_ID'
                )
        
        )
        

        【讨论】:

          猜你喜欢
          • 2016-10-22
          • 2016-09-23
          • 2012-09-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多