简体   繁体   English

无法在Laravel Passport中使用令牌授权

[英]Can't use token authorization with Laravel Passport

I'm setting up Laravel Passport to work like as an api, but even though I get token data through oauth/token I can't seem to use it with curl. 我正在将Laravel Passport设置为像api一样工作,但是即使我通过oauth / token获取令牌数据,我似乎也无法在curl上使用它。

I'm using curl to connect because most of the applications that will be connecting have already been made and many of them use basic php. 我正在使用curl进行连接,因为将要进行连接的大多数应用程序已经制作完毕,其中许多使用基本的php。

The following code gets me a token_type and an access_token from my laravel app. 以下代码从我的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));

But when I try to use the token data to connect to a page, I always get back the message {"message":"Unauthenticated."} 但是,当我尝试使用令牌数据连接到页面时,我总是会收到消息{"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));

My routes/api.php is pretty basic 我的routes / api.php很基本

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

and if I remove the middleware part I can connect. 如果删除中间件,则可以连接。 So, what am I doing wrong on the authentification? 那么,我在身份验证上做错了什么?

You're using variables inside a string with single quotes so the actual variables are never parsed. 您在字符串内使用单引号括起来的变量,因此不会解析实际的变量。 You should use double quotes if you want the value to be interpolated. 如果要插入值,则应使用双引号。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM