Guzzle PHP client response error / invalid request 400 Google OAuth2 talk

Guzzle Request

try {
    $url = 'https://www.googleapis.com/oauth2/v1/tokeninfo?';

    $client = new Client();
    $request = $client->createRequest('GET', $url);

    $query = $request->getQuery();
    $query['access_token'] = $access_token;

    $response = $client->send($request);

    $json = $response->json();

    if(!empty($json) && !isset($json['error'])) {
        return ($json['audience']==GOOGLE_CLIENT_ID);
    }

} catch(\Exception $e) {
    echo $e->getMessage();
}

Buzz response

Client error response
[status code] 400
[reason phrase] Bad Request
[url] https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=xxxx

Simple CURL request

$url = 'https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=xxxx';
$curl_handle=curl_init();
curl_setopt($curl_handle,CURLOPT_URL,$url);
curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,true);
curl_setopt($curl_handle, CURLOPT_SSL_VERIFYPEER, false); //disable SSL check
$json_response = curl_exec($curl_handle);
curl_close($curl_handle);
$response = json_decode($json_response);
return $response;

The simple answer is CURL

stdClass Object
(
    [issued_to] => xxx-xxx.apps.googleusercontent.com
    [audience] => xxx-xxx.apps.googleusercontent.com
    [user_id] => xxx
    [scope] => https://www.googleapis.com/auth/plus.login https://www.googleapis.com/auth/plus.me
    [expires_in] => 3581
    [access_type] => offline
)

I cannot understand what I am doing wrong with Guzzle, as you can see that I got a successful result using CURL, but got a Bad Request error on Guzzle .... Any ideas?

UPDATE:

I find out that guzzle returns the actual answer when the response code is 200 / OK, otherwise its returning guzzle exception now I can not figure out how to get the actual answer in case of an error?

+4
source share
2 answers

I found a solution to use RequestExceptioninsteadException

try {
  //Google oAuth2 Code   
} catch(RequestException $e) {
  $response = $e->getResponse()->json(); //Get error response body
}
+3
source

, exceptions = false grezzle. . : Guzzle: 400

$guzzle_config = array(
    'defaults' => array(
        'debug' => false,
        'exceptions' => false
        )
    );
0

All Articles