php 使用 GuzzleHttp 进行 HTTP POST 方法调用时进程无响应
php process goes unresponsive when making a HTTP POST method call using GuzzleHttp
我正在使用 guzzle HTTP 客户端在用户成功登录后发出基于密码 g运行t 的访问令牌。我正在使用 oauth 的护照包,我已经完成了所有设置,包括它创建的密码 G运行t 客户端。在我的登录控制器中,我重写了 AuthenticatesUsers
特征的 sendLoginResponse
方法,以便在 email/password 身份验证成功时发出访问令牌
public function sendLoginResponse(Request $request)
{
try {
Log::debug("Auth attempt sucessful, obtaining access_token for user :: ".$request->email);
$client = new Client();
$token_response = $client->post(config('app.url').'/oauth/token', [
'form_params' => [
'grant_type' => 'password',
'client_id' => config('auth.password_grant.client.id'),
'client_secret' => config('auth.password_grant.client.secret'),
'username' => $request->email,
'password' => $request->password,
'scope' => '*',
],
]);
if($token_response->getStatusCode()!=200) {
Log:error("Login failed to generate Access Token");
throw new InvalidCredentialsException();
}
$request->session()->regenerate();
$this->clearLoginAttempts($request);
$data = json_decode((string) $token_response->getBody(), true);
Cookie::queue('refresh_token',$data->refresh_token,config('auth.tokens.refresh.expire.days')*1440);
Log::debug("Adding Bearer token to Authorization header");
return response()->view('dashboard', [
'expires_in' => $data->expires_in
], 200)->header('Authorization', $data->token_type.' '.$data->access_token);
} catch(Exception $e){
Log::error('Error :: '.$e->getMessage());
throw $e;
}
}
当我发出这个 post 请求时,整个 PHP 进程没有响应,并且在任何日志中都没有错误。正好在这一行
$token_response = $client->post($token_url, .......
我 运行 在调试会话中;并且 URL、Client ID 和 Secret 是通过配置属性正确生成的;我能看到的唯一异常是 FileNoFoundException
,当它确实找到任何用于限制登录的缓存键时发生,并且这一切都发生在进行此调用并且应用程序继续对用户进行身份验证之前。
当我通过 Postman 或通过 artisan
tinker
使用相同的参数发出这个请求时,我可以得到 access_token
、refresh_token
和 [=21= 的响应]数据。
使用 'Hit And Trial' 几个小时确实可以为您节省 10 分钟的 'Documentation'。
原来我真的不需要做所有这些繁重的工作 this link 展示了我们如何将 \Laravel\Passport\Http\Middleware\CreateFreshApiToken::class,
添加到 web
中间件 app/http/Kernel.php
这需要关心为第一方应用程序生成 ApiToken,例如我用来消费我自己的 React JS API.
虽然这解决了编写所有这些代码的意图,但我仍然不确定是什么导致了 php 代码中的 access_token 过程无响应。
我正在使用 guzzle HTTP 客户端在用户成功登录后发出基于密码 g运行t 的访问令牌。我正在使用 oauth 的护照包,我已经完成了所有设置,包括它创建的密码 G运行t 客户端。在我的登录控制器中,我重写了 AuthenticatesUsers
特征的 sendLoginResponse
方法,以便在 email/password 身份验证成功时发出访问令牌
public function sendLoginResponse(Request $request)
{
try {
Log::debug("Auth attempt sucessful, obtaining access_token for user :: ".$request->email);
$client = new Client();
$token_response = $client->post(config('app.url').'/oauth/token', [
'form_params' => [
'grant_type' => 'password',
'client_id' => config('auth.password_grant.client.id'),
'client_secret' => config('auth.password_grant.client.secret'),
'username' => $request->email,
'password' => $request->password,
'scope' => '*',
],
]);
if($token_response->getStatusCode()!=200) {
Log:error("Login failed to generate Access Token");
throw new InvalidCredentialsException();
}
$request->session()->regenerate();
$this->clearLoginAttempts($request);
$data = json_decode((string) $token_response->getBody(), true);
Cookie::queue('refresh_token',$data->refresh_token,config('auth.tokens.refresh.expire.days')*1440);
Log::debug("Adding Bearer token to Authorization header");
return response()->view('dashboard', [
'expires_in' => $data->expires_in
], 200)->header('Authorization', $data->token_type.' '.$data->access_token);
} catch(Exception $e){
Log::error('Error :: '.$e->getMessage());
throw $e;
}
}
当我发出这个 post 请求时,整个 PHP 进程没有响应,并且在任何日志中都没有错误。正好在这一行
$token_response = $client->post($token_url, .......
我 运行 在调试会话中;并且 URL、Client ID 和 Secret 是通过配置属性正确生成的;我能看到的唯一异常是 FileNoFoundException
,当它确实找到任何用于限制登录的缓存键时发生,并且这一切都发生在进行此调用并且应用程序继续对用户进行身份验证之前。
当我通过 Postman 或通过 artisan
tinker
使用相同的参数发出这个请求时,我可以得到 access_token
、refresh_token
和 [=21= 的响应]数据。
使用 'Hit And Trial' 几个小时确实可以为您节省 10 分钟的 'Documentation'。
原来我真的不需要做所有这些繁重的工作 this link 展示了我们如何将 \Laravel\Passport\Http\Middleware\CreateFreshApiToken::class,
添加到 web
中间件 app/http/Kernel.php
这需要关心为第一方应用程序生成 ApiToken,例如我用来消费我自己的 React JS API.
虽然这解决了编写所有这些代码的意图,但我仍然不确定是什么导致了 php 代码中的 access_token 过程无响应。