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.

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

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站