2017-10-19 105 views
0

我設置Laravel護照像api一樣工作,但即使通過oauth /令牌獲取令牌數據,我似乎也無法使用捲曲。不能使用令牌授權與Laravel護照

我正在使用curl進行連接,因爲大部分將要連接的應用程序已經完成,其中許多應用程序使用基本的php。

以下代碼從我的laravel應用程序獲取token_type和access_token。

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, "http://127.0.0.1:8000/oauth/token"); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_HTTPHEADER, array ("X-Requested-With: XMLHttpRequest")); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 

$postData = array(
    'client_id' => '6', 
    'client_secret' => 'Cprp9HPTYO7CsZRvv4A8MizNj9h1nLjyF6J1sElZ', 
    'grant_type' => 'client_credentials', 
    'scope' => '*' 
); 

curl_setopt($ch, CURLOPT_POSTFIELDS, $postData); 
$ans = json_decode(curl_exec($ch)); 

但是當我嘗試使用令牌的數據連接到一個頁面,我總是回來的消息{"message":"Unauthenticated."}

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, "http://127.0.0.1:8000/api/test"); 
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'X-Requested-With: XMLHttpRequest', 
    'Accept: application/json', 
    'Authorization: {$ans->token_type} {$ans->access_token}', 
    )); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 

print_r(curl_exec($ch)); 

我的路線/ api.php是非常基本的

Route::get('/test', function() { 
    return "Yes! We're testing!!"; 
})->middleware('client'); 

如果我刪除中間件部分,我可以連接。那麼,我在認證上做錯了什麼?

回答

1

您正在使用單引號在字符串中使用變量,所以實際變量永遠不會被分析。如果要插值,應該使用雙引號。

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'X-Requested-With: XMLHttpRequest', 
    'Accept: application/json', 
    "Authorization: {$ans->token_type} {$ans->access_token}", 
)); 
+0

對不起,我必須走在恥辱掛我的頭。 – Califer