具有常规设计的Passport Laravel Rest API Auth [英] Passport Laravel Rest API Auth With Normal Design
问题描述
我已完全分配了Passport REST API,并获得了令牌并在PostMan上进行了测试
I have Completely assigned the Passport REST API and i get token and tested on PostMan
并使用此Auth检索数据
and it retrieve the data with this Auth
这是带有laravel刀片视图(NOT VUE.JS)的普通js html
this is normal js html with laravel blade view (NOT VUE.JS)
<script>
$.ajax({
method: 'POST',
dataType: 'json',
url: "**********/api/User/GetPost",
headers: {
'Authorization':'Bearer Tokenblablabla',
'Content-Type':'application/json'
},
data: null,
success: function(data){
console.log('succes: '+data);
}
});
这是控制器功能
public function GetPosts(Request $request){
$data3="Test"
return response()->json([
'Success'=> true,
'Message'=>'8',
'Data' => $data3,
], 200);
}
这是api.php//将返回带有帖子详细信息的json
this is api.php // this will return a json with posts details
Route::group(['middleware'=>'auth:api'],function(){
Route::post('/GetPosts','Wall\PostsController@GetPosts');
});
这是web.php
Route::get('/GetPost', function () {
return view('getpostview');
})->middleware('auth:api');
我很困惑,我无法发送Auth,而且我无法从API iam检索json,此问题持续了3天 Iam将普通的laravel刀片连接到另一个laravel护照REST API所需的内容
i get confused i cannot send Auth beside i cannt retrieve the json from API iam 3 days STUCK in this problem what iam need to connect the normal laravel blade to another laravel passport REST API
推荐答案
Laravel生成的用于身份验证的Blade模板可以与Laravel Passport,JWT等一起使用.
The Blade templates generated by Laravel for authentication can works with Laravel Passport, JWT, etc.
您需要做的是正确配置护照.
The thing that you need to do is configure Passport properly.
不要忘记修改API config/auth.php
文件中指定的驱动程序:
Do not forget to modify the driver specified in the API config/auth.php
file:
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'passport',
'provider' => 'users',
],
],
此外,要检索json响应,请在请求中添加此标头:
Also, to retrieve a json response add this header in your requests:
接受":应用程序/json"
'Accept' : 'application/json'
因此您的请求应如下所示:
So your request should be like this:
$response = $client->request('GET', 'some-url/api/getPosts', [
'headers' => [
'Accept' => 'application/json',
'Authorization' => 'Bearer '.$accessToken,
],
]);
旁注
我鼓励您阅读此演示文稿,教狗来REST" ,以使用HTTP动词和良好做法正确创建干净,直观且易于学习的API.
Side note
I encourage you to read this presentation, "Teaching a Dog to REST", to create a clean, intuitive and easy to learn API properly using HTTP verbs and good practices.
这篇关于具有常规设计的Passport Laravel Rest API Auth的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!