2016-08-25 54 views
2

我試圖讓ACCES從Instagram的API憑證令牌是這樣的文檔請求Instagram的API存取權限與捲曲

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' \ 
    https://api.instagram.com/oauth/access_token 

的樣品要求,這是我的代碼

<body> 
    <?php 
     $curl = curl_init(); 
     curl_setopt($curl, CURLOPT_URL, "https://api.instagram.com/oauth/access_token"); 
     curl_setopt($curl,CURLOPT_POST, true); 
     curl_setopt($curl,CURLOPT_POSTFIELDS, "client_id=MYID& 
     client_secret=MY_CLIENT_SECRET&grant_type=authorization_code& 
     redirect_uri=http://localhost/pruebainst/pruebas.php&code=".$_GET['code']); 
     curl_setopt($curl, CURLOPT_RETURNTRANSFER,true); 
     curl_setopt($curl, CURLOPT_TIMEOUT, 10); 
     $output = curl_exec($curl); 
     curl_close($curl); 

     echo ($output); 
    ?> 
    </body> 

隨着此代碼我沒有得到任何捲曲請求

回答

2

使用此代碼將爲您提供重定向URL中的訪問令牌。

$client_id = 'YOUR CLIENT ID'; 
    $client_secret ='YOUR CLIENT SECRET'; 
    $redirect_uri = 'YOUR REDIRECT URI'; 

    $auth_request_url = 'https://api.instagram.com/oauth/authorize/?client_id='.$client_id.'&redirect_uri='.$redirect_uri .'&response_type=token'; 
/* Send user to authorisation */ 
header("Location: ".$auth_request_url); 

另外如果你想訪問令牌編程方式使用這一個

$client_id = 'YOUR CLIENT ID'; 
    $client_secret ='YOUR CLIENT SECRET'; 
     $redirect_uri = 'YOUR REDIRECT URI'; 
    $code ='Enter your code manually'; 

    $url = "https://api.instagram.com/oauth/access_token"; 
    $access_token_parameters = array(
     'client_id'    =>  $client_id, 
     'client_secret'   =>  $client_secret, 
     'grant_type'    =>  'authorization_code', 
     'redirect_uri'    =>  $redirect_uri, 
     '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 

    var_dump($result); 

注:$ url和$ REDIRECT_URI不相同