不能使用令牌授权与Laravel护照
问题描述:
我设置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');
如果我删除中间件部分,我可以连接。那么,我在认证上做错了什么?
答
您正在使用单引号在字符串中使用变量,所以实际变量永远不会被分析。如果要插值,应该使用双引号。
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'X-Requested-With: XMLHttpRequest',
'Accept: application/json',
"Authorization: {$ans->token_type} {$ans->access_token}",
));
对不起,我必须走在耻辱挂我的头。 – Califer