3

次の 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 を返します。これが機能しない理由がわかりません。

誰かがこのコードをデバッグして、私のコードの問題を教えてください。

4

3 に答える 3

3

すでにhttpsなので、これをコードに配置する必要があります

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

リダイレクト uri がローカルにあるかどうかではありません。curl オプションに SSL_VERIFYPEER が設定されていないためです。

于 2017-03-04T12:03:43.433 に答える
0

あなたのコードにはいくつかの問題があります。1 つには、redirect_uri が localhost のようです。明らかに、Instagram への localhost は独自のサーバーです。FQDN/世界からアクセス可能な URL を提供する必要があります。

また、http://php.net/manual/en/function.curl-setopt.phpからコード

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

シンプルであるべき

curl_setopt($ch, CURLOPT_POST, 1);
于 2013-03-29T20:48:25.103 に答える
0

あなたのコードは正しいですが、変更が必要です

$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-03-11T10:50:45.607 に答える