1

我正在尝试使用 Satellizer 和angularjs 作为前端,使用 laravel & Guzzle 6 作为后端的社交验证教程。我在使用 Guzzle 查询用户个人资料信息时遇到问题:这是我的控制器中的 php 代码:

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

我无法连接以获取用户配置文件和错误消息:

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
.......

有人有同样的问题并找到解决方法吗?请帮忙。谢谢。

4

1 回答 1

0

对于任何有同样问题的人,都试图遵循 Laravel 和 php 的 Satellizer 示例。我更改了 Google API url(来自文档),现在我可以获取用户个人资料信息。

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

示例中的链接可能适用于旧版本。

于 2015-10-08T01:40:00.800 回答