0

これは、Googleでユーザーを認証し、基本的なものを表示するためのコードです。

Google でユーザーを認証中にエラーが発生する

エラー : oauth 2.0 google + api の認証情報が無効で、apiServiceException をスローしています

これは私のコードです:

<?php
require_once 'google-api-php-client/src/apiClient.php';
require_once 'google-api-php-client/src/contrib/apiPlusService.php';
require_once 'google-api-php-client/src/contrib/apiOauth2Service.php';

session_start();

$client = new apiClient();

$client->setApplicationName('demo');
$client->setClientId('asd6354egfdgtdewd');
$client->setClientSecret('-ooRVhB5nbdsfisfgf7s6fsfsfj');
$client->setRedirectUri('http://demo.com');
$client->setDeveloperKey('HGFHJVhjb894rbbvjhdfjdsvkbdvdv');

$client->setScopes(array('https://www.googleapis.com/auth/plus.me',
                             'https://www.googleapis.com/auth/userinfo.email'));

$plus   = new apiPlusService($client);
$oauth2 = new apiOauth2Service($client);

if (isset($_REQUEST['logout']))
{
    unset($_SESSION['access_token']);
}

if (isset($_GET['code']))
{
    $client->authenticate();
    $_SESSION['access_token'] = $client->getAccessToken();
    header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']);
            # FIXME Is exit() missing?
}

if (isset($_SESSION['access_token']))
{
    $client->setAccessToken($_SESSION['access_token']);
}

if ($client->getAccessToken())
{
        $me   = $plus->people->get('me');
        $user = $oauth2->userinfo->get();

            $email = filter_var($user['email'], FILTER_SANITIZE_EMAIL);

            $optParams = array('maxResults' => 100);
        $activities = $plus->activities->listActivities('me', 'public',$optParams);

        $_SESSION['access_token'] = $client->getAccessToken();

    } else {

           $authUrl = $client->createAuthUrl();

}
4

1 に答える 1

0

andsetAccessToken($token)を呼び出す直前に関数を呼び出す必要があります。$plus->people->get('me')$user = $oauth2->userinfo->get()

于 2015-05-08T00:29:28.113 に答える