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

使用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 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了