2017-08-02 141 views
0

我想,當我在終端使用PHP從BOX API獲取令牌?

curl https://api.box.com/oauth2/token \ 
-d 'grant_type=authorization_code&code=CODE&client_id=CLIENT_ID&client_secret=secret_ID' \ 
-X POST # This is working. 

一個以上運行它擺脫箱API下面捲曲工作訪問令牌是工作,但我試圖用PHP但同樣的事情是投以下錯誤{"error":"invalid_request","error_description":"Invalid grant_type parameter or parameter missing"}1下面的代碼我試圖

$access_token_url = "https://api.box.com/oauth2/token"; 
$ch = curl_init(); 
curl_setopt($ch,CURLOPT_URL, $access_token_url); 
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'grant_type'=>'authorization_code', 
    'code'=>'code', 
    'client_id'=>'id', 
    'client_secret'=>'secret' 
    )); 
$response = curl_exec($ch); 
curl_close($ch); 

我不知道什麼是真正的問題。

回答

1

必須設置爲POST參數,而不是HEADER參數

$access_token_url = "https://api.box.com/oauth2/token"; 
$ch = curl_init(); 
curl_setopt($ch,CURLOPT_URL, $access_token_url); 
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query([ 
    'grant_type'=>'authorization_code', 
    'code'=>'code', 
    'client_id'=>'id', 
    'client_secret'=>'secret' 
])); 
$response = curl_exec($ch); 
curl_close($ch); 
0

您正在發送不同的參數。 -d option發送一個POST請求,所以你不能在一個GET中混合所有的參數。執行要求在所提供的example

curl https://api.box.com/oauth2/token \ 
-d 'grant_type=authorization_code' \ 
-d 'code=<MY_AUTH_CODE>' \ 
-d 'client_id=<MY_CLIENT_ID>' \ 
-d 'client_secret=<MY_CLIENT_SECRET>' \ 
-X POST 
1

我認爲這是因爲命令行例子做一個POST請求的數據但PHP捲曲請求不是。下面的代碼應該有希望讓你走上正軌。

<?php 
$access_token_url = "https://api.box.com/oauth2/token"; 
$ch = curl_init(); 
curl_setopt($ch,CURLOPT_URL, $access_token_url); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, 
    array(
    'grant_type'=>'authorization_code', 
    'code'=>'code', 
    'client_id'=>'id', 
    'client_secret'=>'secret')); 

$response = curl_exec($ch); 
curl_close($ch); 
?>