laravel 护照登录总是在应用程序中失败但成功但在邮递员中成功
laravel passport login always failed in apps but success but success in postman
在 laravel 方面需要一些帮助。
我在API控制器
中有这个
public function login(Request $request)
{
$credentials = [
'email' => $request->email,
'password' => $request->password
];
if (auth()->attempt($credentials)) {
$token = auth()->user()->createToken('myToken')->accessToken;
return response()->json(['token' => $token], 200);
} else {
return response()->json(['error' => 'UnAuthorised'], 401);
}
}
当我从邮递员登录时,我得到了授权。
但是当我从应用程序登录失败时,它 return
GuzzleHttp \ Exception \ ClientException (401)
Client error: POST https://api.mydomain.com/api/login
resulted in a 401 Unauthorized
response: {"error":"UnAuthorised"}
调用登录的代码
$group = 'all';
$start_date = $request->start_date;
$end_date = $request->end_date;
$date1 = new DateTime($start_date);
$date2 = new DateTime($end_date);
$interval = $date1->diff($date2);
$count_days = $interval->days;
$health_care = Auth::user()->health_care_id;
$request->validate([
'start_date' => 'required',
'end_date' => 'required',
]);
$client = new Client();
$token = 'some-token-i-got-from-postman-login-result';
$client = new \GuzzleHttp\Client();
$cookieJar = new \GuzzleHttp\Cookie\CookieJar();
$response = $client->post('https://api.mydoman.com/api/login', [
'form_params' => [
'email' => 'name@mydoman.com',
'password' => 'mypassword',
'action' => 'login',
],
'cookies' => $cookieJar,
]);
我的composer.json
{
"name": "laravel/laravel",
"type": "project",
"description": "The Laravel Framework.",
"keywords": [
"framework",
"laravel"
],
"license": "MIT",
"require": {
"php": "^7.2",
"fideloper/proxy": "^4.0",
"laravel/framework": "^6.2",
"laravel/passport": "^8.0",
"laravel/tinker": "^1.0"
},
"require-dev": {
"facade/ignition": "^1.4",
"fzaninotto/faker": "^1.4",
"mockery/mockery": "^1.0",
"nunomaduro/collision": "^3.0",
"phpunit/phpunit": "^8.0"
},
"config": {
"optimize-autoloader": true,
"preferred-install": "dist",
"sort-packages": true
},
"extra": {
"laravel": {
"dont-discover": []
}
},
"autoload": {
"psr-4": {
"App\": "app/"
},
"classmap": [
"database/seeds",
"database/factories"
]
},
"autoload-dev": {
"psr-4": {
"Tests\": "tests/"
}
},
"minimum-stability": "dev",
"prefer-stable": true,
"scripts": {
"post-autoload-dump": [
"Illuminate\Foundation\ComposerScripts::postAutoloadDump",
"@php artisan package:discover --ansi"
],
"post-root-package-install": [
"@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
],
"post-create-project-cmd": [
"@php artisan key:generate --ansi"
]
}
}
我运行前台和网页API在同一台服务器上。
建立在 laragon 之上(Apache 和 php 7.2)
请帮忙
使用json
代替form_params
:
'json' => [
'email' => 'name@mydoman.com',
'password' => 'mypassword',
'action' => 'login',
]
这很可能是环境问题,因为我的 API 在 Live 服务器中运行良好。所以我结束了将我的 LIVE API 克隆到 LIVE DEV-API 中。
原因可能是因为在 windows 上的本地 i 运行 但在 linux 上的 LIVE i 运行,所以我想我需要做一些调整以使其在 [= =15=]。
所以原因仍然没有解决,但至少我知道它是一些环境问题。
谢谢你们的帮助,希望这对其他人有帮助
其他解决方案:
运行 使用“php artisan serve”而不是使用 webserver
在 laravel 方面需要一些帮助。
我在API控制器
中有这个 public function login(Request $request)
{
$credentials = [
'email' => $request->email,
'password' => $request->password
];
if (auth()->attempt($credentials)) {
$token = auth()->user()->createToken('myToken')->accessToken;
return response()->json(['token' => $token], 200);
} else {
return response()->json(['error' => 'UnAuthorised'], 401);
}
}
当我从邮递员登录时,我得到了授权。 但是当我从应用程序登录失败时,它 return
GuzzleHttp \ Exception \ ClientException (401) Client error:
POST https://api.mydomain.com/api/login
resulted in a401 Unauthorized
response: {"error":"UnAuthorised"}
调用登录的代码
$group = 'all';
$start_date = $request->start_date;
$end_date = $request->end_date;
$date1 = new DateTime($start_date);
$date2 = new DateTime($end_date);
$interval = $date1->diff($date2);
$count_days = $interval->days;
$health_care = Auth::user()->health_care_id;
$request->validate([
'start_date' => 'required',
'end_date' => 'required',
]);
$client = new Client();
$token = 'some-token-i-got-from-postman-login-result';
$client = new \GuzzleHttp\Client();
$cookieJar = new \GuzzleHttp\Cookie\CookieJar();
$response = $client->post('https://api.mydoman.com/api/login', [
'form_params' => [
'email' => 'name@mydoman.com',
'password' => 'mypassword',
'action' => 'login',
],
'cookies' => $cookieJar,
]);
我的composer.json
{
"name": "laravel/laravel",
"type": "project",
"description": "The Laravel Framework.",
"keywords": [
"framework",
"laravel"
],
"license": "MIT",
"require": {
"php": "^7.2",
"fideloper/proxy": "^4.0",
"laravel/framework": "^6.2",
"laravel/passport": "^8.0",
"laravel/tinker": "^1.0"
},
"require-dev": {
"facade/ignition": "^1.4",
"fzaninotto/faker": "^1.4",
"mockery/mockery": "^1.0",
"nunomaduro/collision": "^3.0",
"phpunit/phpunit": "^8.0"
},
"config": {
"optimize-autoloader": true,
"preferred-install": "dist",
"sort-packages": true
},
"extra": {
"laravel": {
"dont-discover": []
}
},
"autoload": {
"psr-4": {
"App\": "app/"
},
"classmap": [
"database/seeds",
"database/factories"
]
},
"autoload-dev": {
"psr-4": {
"Tests\": "tests/"
}
},
"minimum-stability": "dev",
"prefer-stable": true,
"scripts": {
"post-autoload-dump": [
"Illuminate\Foundation\ComposerScripts::postAutoloadDump",
"@php artisan package:discover --ansi"
],
"post-root-package-install": [
"@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
],
"post-create-project-cmd": [
"@php artisan key:generate --ansi"
]
}
}
我运行前台和网页API在同一台服务器上。
建立在 laragon 之上(Apache 和 php 7.2)
请帮忙
使用json
代替form_params
:
'json' => [
'email' => 'name@mydoman.com',
'password' => 'mypassword',
'action' => 'login',
]
这很可能是环境问题,因为我的 API 在 Live 服务器中运行良好。所以我结束了将我的 LIVE API 克隆到 LIVE DEV-API 中。 原因可能是因为在 windows 上的本地 i 运行 但在 linux 上的 LIVE i 运行,所以我想我需要做一些调整以使其在 [= =15=]。 所以原因仍然没有解决,但至少我知道它是一些环境问题。 谢谢你们的帮助,希望这对其他人有帮助
其他解决方案: 运行 使用“php artisan serve”而不是使用 webserver