简体   繁体   中英

Guzzle 6 with google OAuth not working.?

I'm trying to follow a tutorial here using Satellizer and angularjs as front-end and laravel & Guzzle 6 for back-end with social authentication . I have problem using Guzzle to query user profile information: here is my php code in my controller :

$accessTokenUrl = 'https://accounts.google.com/o/oauth2/token';
    $peopleApiUrl = 'https://www.googleapis.com/plus/v1/people/me/openIdConnect';
    $params = [
        'code' => $request->input('code'),
        'client_id' => $request->input('clientId'),
        'client_secret' => 'my-client-secret',
        'redirect_uri' => $request->input('redirectUri'),
        'grant_type' => 'authorization_code',
    ];
    $client = new GuzzleHttp\Client();
    $accessTokenResponse = $client->post($accessTokenUrl, ['form_params' => $params]);
    $accessToken = json_decode($accessTokenResponse->getBody()->getContents(),true)['access_token'];

    try {

//            $profileResponse = $client->get($peopleApiUrl, ['header' => ['Authorization' => 'Bearer ' . $accessToken,]]); -->not working
            $profileResponse = $client->get($peopleApiUrl,['oauth_token'=>$accessToken]);
        } catch (RequestException $e) {
            return response()->json($e);
        }

I cannot connect to get user profile and the error msg :

ClientException in Middleware.php line 69: Client error: 403

in Middleware.php line 69
at Middleware::GuzzleHttp\{closure}(object(Response)) in Promise.php line 199
at Promise::callHandler('1', object(Response), array(object(Promise), object(Closure), null)) in Promise.php line 152
at Promise::GuzzleHttp\Promise\{closure}() in TaskQueue.php line 60
at TaskQueue->run() in CurlMultiHandler.php line 96
at CurlMultiHandler->tick() in CurlMultiHandler.php line 123
at CurlMultiHandler->execute(true) in Promise.php line 240
at Promise->invokeWaitFn() in Promise.php line 217
at Promise->waitIfPending() in Promise.php line 261
at Promise->invokeWaitList() in Promise.php line 219
at Promise->waitIfPending() in Promise.php line 62
.......

anyone have same problem and found a way to fix it ? please help. thanks.

For anyone who have the same issue trying to follow Satellizer example for Laravel and php. I change the Google API url (from the doc) and now I can get the user profile info.

$peopleApiUrl = 'https://www.googleapis.com/oauth2/v3/userinfo';  // works with this link

the link for in the example maybe for older version .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM