Laravel 5.6 Passport oAuth2无法使用Guzzle发出请求 [英] Laravel 5.6 Passport oAuth2 can't make request with Guzzle
问题描述
我在Laravel 5.6上有一个新项目,我试图在此研究和了解Passport的API Auth.我正在尝试这样做,然后在要访问该API的地方创建一个 Javascript
应用程序.因此,第一方应用程序的API.
I have a fresh project on Laravel 5.6, where I'm trying to study and understand API Auth with Passport. I'm trying to do that, and after that to make a Javascript
application from where I'll access that API. So, API for first-party applications.
我已经安装并注册了所有针对护照的路线和设置,还安装了 Guzzle
.
I've installed and registered all routes and setup specific to passport, and also installed Guzzle
.
我寻找了一些教程,现在我有了该代码:
I looked for some tutorials and now I'm with that code :
RegisterController.php
<?php
namespace App\Http\Controllers\Api\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use Laravel\Passport\Client;
use App\User;
class RegisterController extends Controller
{
use IssueTokenTrait;
private $client;
public function __construct(){
$this->client = Client::find(1); //Client 1 is a Laravel Password Grant Client token from my DB (when I wrote php artisan passport:install
}
public function register(Request $request){
$this->validate($request, [
'name' => 'required',
'email' => 'required|email|unique:users,email',
'password' => 'required|min:3',
'password_confirmation' => 'required|same:password'
]);
$user = User::create([
'name' => request('name'),
'email' => request('email'),
'password' => bcrypt(request('password'))
]);
return $this->issueToken($request, 'password');
}
}
它使用该Trait中的 issueToken
函数:
It uses issueToken
function from that Trait :
IssueTokenTrait.php
namespace App\Http\Controllers\Api\Auth;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use GuzzleHttp\Client;
trait IssueTokenTrait{
public function issueToken(Request $request, $grantType, $scope = ""){
$params = [
'grant_type' => $grantType,
'client_id' => $this->client->id,
'client_secret' => $this->client->secret,
'scope' => $scope
];
$params['username'] = $request->username ?: $request->email;
$request->request->add($params);
$proxy = Request::create('oauth/token', 'POST');
return Route::dispatch($proxy);
}
}
**现在的问题:**一切正常.我可以注册,我有一个访问令牌,该令牌可用于受 auth
路由保护的访问令牌,但是当我输入错误的令牌时不起作用.我阅读了 Laravel 5.6
中的 Passport
文档,并且所有示例都使用 GuzzleHttp
在控制器方法内发出请求,并且我尝试重写我的代码使用 Guzzle
而不是 Request :: dispatch
.
**NOW THE PROBLEM : **
Everything works perfect. I can register, I have an access token which works on protected with auth
routes, and doesn't work when I give a wrong token.
I read the documentation of Passport
in Laravel 5.6
and all examples use GuzzleHttp
to make requests inside controller method, and I have tried to rewrite my code using Guzzle
instead of Request::dispatch
.
因此,我在多个资源中找到了文档以及具有不同但逻辑实现相同的代码,因此我的 IssueTokenTrait 现在看起来像:
So, I found in multiple sources, in documentation as well code with different but also same logic implementation, so my IssueTokenTrait now looks like :
<?php
namespace App\Http\Controllers\Api\Auth;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use GuzzleHttp\Client;
trait IssueTokenTrait{
public function issueToken(Request $request, $grantType, $scope = ""){
$params = [
'grant_type' => $grantType,
'client_id' => $this->client->id,
'client_secret' => $this->client->secret,
'scope' => $scope
];
$params['username'] = $request->username ?: $request->email;
$url = url('/oauth/token');
$headers = ['Accept' => 'application/json'];
$http = new GuzzleHttp\Client;
$response = $http->post($url, [
'headers' => $headers,
'form_params' => [
'grant_type' => 'password',
'client_id' => $this->client->id,
'client_secret' => $this->client->secret,
'username' => $request->email,
'password' => $request->password
],
]);
return json_decode((string)$response->getBody(), true);
}
}
这是我的应用程序崩溃的原因.现在,当我从 POSTMAN
向/api/register
发送 POST
请求时,它只是没有返回我一个响应,例如 please wait...
就这样.如果我重新启动服务器,它将返回我: [2018年8月20日星期一11:29:16]无法收听127.0.0.1:8000(原因:地址已在使用中)
.因此,看起来它发出了此请求,但它没有返回响应,或者陷入了无限循环.
And there is how my app gets broken.
When I make a POST
request to /api/register
from POSTMAN
now, it just not returns me a response, like please wait...
and that's it. And if I restart my server, it returns me :
[Mon Aug 20 11:29:16 2018] Failed to listen on 127.0.0.1:8000 (reason: Address already in use)
.
So, it looks like it makes this request, but it not returns the response, or it goes in a infinite loop.
这个问题困扰了我整整一天,实际上看起来有些神秘.由于所有参数和值都与 Route :: dispatch
相同,因此更改此 HTTP
请求的方法.
I get stuck for a day with that problem, and really it looks like some mystic here. Because all parameters and values are like it was with Route::dispatch
, just the method of making this HTTP
request changes.
推荐答案
有2个选项:
- 尝试使用不同的端口(例如:8001)运行另一个Artisan服务器进程,并大吃一惊.
- 改为使用个人访问令牌,使用
createToken
生成访问令牌.
- Try to run another Artisan server process with different port (ex: 8001) and guzzle to it.
- Using personal access token instead, using
createToken
to generate access token.
这篇关于Laravel 5.6 Passport oAuth2无法使用Guzzle发出请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!