2015-02-06 77 views
0
的access_token

我初學編程的的Instagram獲得訪問令牌聲明:未定義指數:

讓Instagram的認證,我上傳這個PHP代碼,我的服務器

<?php 

getAccessToken(); 

function getAccessToken(){ 

if($_GET['code']) { 
    $code = $_GET['code']; 
    $url = "https://api.instagram.com/oauth/access_token"; 
    $access_token_parameters = array(
      'client_id'    =>  'b7f14786b7754f04aa43dcxxxxxxxxxx', // my client id 
      'client_secret'   =>  '33987fcae22949a9b3682bxxxxxxxxxx', // secret key 
      'grant_type'    =>  'authorization_code', 
      'redirect_uri'    =>  'http://thedreamkr.com/', 
      'code'      =>  $code 
    ); 
    $curl = curl_init($url); // we init curl by passing the url 
    curl_setopt($curl,CURLOPT_POST,true); // to send a POST request 
    curl_setopt($curl,CURLOPT_POSTFIELDS,$access_token_parameters); // indicate the data to send 
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // to return the transfer as a string of the return value of curl_exec() instead of outputting it out directly. 
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // to stop cURL from verifying the peer's certificate. 
    $result = curl_exec($curl); // to perform the curl session 
    curl_close($curl); // to close the curl session 

    $arr = json_decode($result,true); 
    echo "access_Token: ".$arr['access_token']; // display the access_token 
    echo "<br>"; 
    echo "user_name : ".$arr['user']['username']; // display the username 
} 
} 

?> 

上,當我訪問「https://api.instagram.com/oauth/authorize/?client_id=CLIENT-ID&redirect_uri=REDIRECT-URI&response_type=code

返回以下錯誤

Notice: Undefined index: access_token in /home/ubuntu/php/hashberry/token.php on line 26 
access_Token: 
Notice: Undefined index: user in /home/ubuntu/php/hashberry/token.php on line 28 
user_name : 

回答

2

房顫ter測試,你的代碼完美工作。

但是: 1 - 你需要有重定向URI是從您的網站URI不同的內部代碼。 2-更改您的重定向URI相匹配的一個在您註冊的應用程序。

例如: 網站URI:https://example.com/
重定向URI:https://example.com/register/

好運