Laravel护照试图获取oauth/令牌. Guzzle保持挂起状态,并且还在其他Guzzle通话中 [英] Laravel passport trying to get oauth/token. Guzzle keeps hanging, also on other Guzzle calls

查看:119
本文介绍了Laravel护照试图获取oauth/令牌. Guzzle保持挂起状态,并且还在其他Guzzle通话中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Laravel 5.8设置一个新项目,我还安装了Laravel Passport& uzz.使用php artisan服务在端口8000上创建了一个本地服务器.Postman能够在/oauth/token上获得响应(但仅当主体为JSON时).

Set up a new project with Laravel 5.8 and I also installed Laravel Passport & Guzzle. Created a local server with php artisan serve on port 8000. Postman is able to get a response at /oauth/token (but only when the body is in JSON).

当我尝试通过控制器方法访问API时,浏览器返回500错误,并且日志中未显示任何错误.当我尝试通过路由将食人魔指向同一控制器方法时,邮递员不断加载,甚至需要强制关闭我的php进程.当我尝试使用Guzzle向API(另一条路线)发出get请求时,它也一直挂在邮递员手中.如果我直接在邮递员中定位路线,则会显示内容. Axios还会返回所需的内容.

When I try to access the API through a controller method my browser returns a 500 error and no errors are showing up in the logs. When I try to point guzzle to the same controller method through a route postman keeps loading and it's even needed to force close my php process. When I try to make a get request to the API (another route) with Guzzle it also keeps hanging in postman. If I target the route directly in postman it shows the contents. Axios also returns the contents needed.

$http = new Client();

$result = $http->post('http://localhost:8000' . '/oauth/token', [
    'form_params' => [
        'grant_type' => 'password',
        'client_id' => config('app.api_client'),
        'client_secret' => config('app.api_secret'),
        'username' => $user->email,
        'password' => $password,
    ],
]);

我也尝试过

$http = new Client();

$result = $http->post('/oauth/token', [
    'form_params' => [
        'base_uri' => config('app.url'),
        'headers' => [
            'Accept' => 'application/json',
        ],
        'grant_type' => 'password',
        'client_id' => config('app.api_client'),
        'client_secret' => config('app.api_secret'),
        'username' => $user->email,
        'password' => $password,
    ],
]);

我还尝试了带有标头的第一个版本,并且都标有scope => '',但是没有运气.

And I also tried the first version with the headers, and both with scope => '', but no luck.

有人知道我在做什么吗?

Does anyone know what I'm doing wrong?

推荐答案

问题是使用php artisan serve时,它使用的PHP服务器为

The issue is when using php artisan serve, it uses a PHP server which is single-threaded.

Web服务器仅运行一个单线程进程,因此如果请求被阻止,PHP应用程序将停止运行.

The web server runs only one single-threaded process, so PHP applications will stall if a request is blocked.

您可以执行此解决方案:

在对其自身进行调用时,线程将阻塞以等待其自己的回复.解决方案是将提供的应用程序和使用的应用程序分离到各自的实例中,或者在诸如Apache或nginx的多线程Web服务器上运行它.

When making calls to itself the thread blocked waiting for its own reply. The solution is to either seperate the providing application and consuming application into their own instance or to run it on a multi-threaded webserver such as Apache or nginx.

或者,如果您正在寻找快速修复程序来测试您的更新-您可以通过打开两个命令提示符来完成此操作.第一个将运行php artisan serve(在本地,我的默认端口是8000,而您将在http://localhost:8000上运行您的站点).第二个将运行php artisan serve --port 8001.

Or if you are looking for a quick fix to test your updates - you can get this done by opening up two command prompts. The first would be running php artisan serve (locally my default port is 8000 and you would be running your site on http://localhost:8000). The second would run php artisan serve --port 8001.

然后,您将发布请求更新为:

Then you would update your post request to:

$result = $http->post('http://localhost:8001' . '/oauth/token', [
    'form_params' => [
        'grant_type' => 'password',
        'client_id' => config('app.api_client'),
        'client_secret' => config('app.api_secret'),
        'username' => $user->email,
        'password' => $password,
    ],
]);

这应该在测试期间有所帮助,直到您能够访问服务器或本地虚拟主机上的所有内容为止.

This should help during your testing until you are able to everything on server or a local virtual host.

这篇关于Laravel护照试图获取oauth/令牌. Guzzle保持挂起状态,并且还在其他Guzzle通话中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆