dtxooq1020 2019-06-11 06:01
浏览 501
已采纳

使用GuzzleHttp进行HTTP POST方法调用时,php进程无响应

I am using guzzle HTTP client to issue a password grant based access token after successful user login. I am using passport package for oauth and I have done all the setup which includes the Password Grant Client it creates. In my login controller I override the sendLoginResponse method of the AuthenticatesUsers trait so as to issue an access token on successful email/password authentication

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;
    }
}

The whole PHP process goes unresponsive when I make this post request, and there are no errors in any of the logs. Exactly at this line

$token_response = $client->post($token_url, .......

I ran this in Debug session; and the URL, Client ID and Secret are generated correctly via configuration properties; the only Exception I could see was a FileNoFoundException that occurs when it does find any cache key for throttle login and that all happens much before this call is made and the app proceeds to authenticate the user.

When I make this very request with same parameters through Postman or via artisan tinker I can get a response with access_token, refresh_token and expires_in data.

  • 写回答

1条回答 默认 最新

  • douzhuolong9886 2019-06-12 07:03
    关注

    A couple of hours with 'Hit And Trial' does really save you 10 minutes of going through 'Documentation'.

    Turns out I really don't have to do all this heavy lifting this link shows how we can add the \Laravel\Passport\Http\Middleware\CreateFreshApiToken::class, to web middleware in app/http/Kernel.php which takes care of generating ApiToken for first party apps such as the React JS which I'd be using to consume my own API.

    While that solved the intent of writing all this code, I'm still not sure what was causing the process unresponsiveness making the access_token from within the php code.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 虚心请教几个问题,小生先有礼了
  • ¥30 截图中的mathematics程序转换成matlab