Composer和Php Google Api客户端

时间:2015-03-26 02:02:46

标签: composer-php google-api-php-client google-api-client

这是我的composer.json:

 {
    "require": {
        "google/apiclient": "1.0.*@beta"
    }
}

这是我的代码:

<?
$path = get_include_path() . PATH_SEPARATOR . 'C:\wamp\www\gCalendar\vendor\google\apiclient\src';
set_include_path($path);
define("APIKEY","AIxxxxxxxWA");
define("CLIENTID","xxxkqt.apps.googleusercontent.com");
define("CLIENTSECRET","xxxx");
define("DEVELOPERKEY","xxx.apps.googleusercontent.com");

require_once("config.php");
require_once("vendor/autoload.php");

session_start();

$scriptUri = "http://".$_SERVER["HTTP_HOST"].$_SERVER['PHP_SELF'];

$client = new Google_Client();
$client->setAccessType('online'); // default: offline
$client->setApplicationName('CalendarTest');
$client->setClientId(CLIENTID);
$client->setClientSecret(CLIENTSECRET);
$client->setRedirectUri($scriptUri);
$client->setDeveloperKey(APIKEY); // API key

// $service implements the client interface, has to be set before auth call
$service = new Google_AnalyticsService($client);

if (isset($_GET['logout'])) { // logout: destroy token
    unset($_SESSION['token']);
    die('Logged out.');
}

if (isset($_GET['code'])) { // we received the positive auth callback, get the token and store it in session
    $client->authenticate();
    $_SESSION['token'] = $client->getAccessToken();
}

if (isset($_SESSION['token'])) { // extract token from session and configure client
    $token = $_SESSION['token'];
    $client->setAccessToken($token);
}

if (!$client->getAccessToken()) { // auth call to google
    $authUrl = $client->createAuthUrl();
    header("Location: ".$authUrl);
    die;
}
echo 'Hello, world.';
?>

我已经回复了这个错误:

( ! ) Fatal error: Class 'Google_AnalyticsService' not found in C:\wamp\www\gCalendar\index.php on line 21

我做错了什么,包括使用Composer的库?

非常感谢

4 个答案:

答案 0 :(得分:1)

该库中不存在类Google_AnalyticsService。请改为Google_Service

$service = new Google_Service($client);

答案 1 :(得分:0)

我知道这已经过时了,但我看不到任何答案,而且那里的内容还不够......将范围设置为“https://www.googleapis.com/auth/analytics”有帮助吗?

此处找到所有范围:

https://developers.google.com/identity/protocols/googlescopes

<?php
    session_start();
    $_SESSION = [];
    require_once 'vendor/autoload.php';//Composer generated autoload.php(not Google/autoload.php)
    $google_api_key = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
    $clientID = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA.apps.googleusercontent.com";
    $clientSecret = "AAAAAAAAAAAAAAAAAAAAAAAA";
    $scriptUri = "https://".$_SERVER["HTTP_HOST"].$_SERVER['PHP_SELF'];
    $client = new Google_Client();
    $client->setAccessType('online');
    $client->setApplicationName('MYAPPNAME');
    $client->setClientId($clientID );
    $client->setClientSecret($clientSecret);
    $client->setRedirectUri($scriptUri);
    $client->setDeveloperKey($google_api_key);
    $client->addScope('https://www.googleapis.com/auth/analytics');
    $service = new Google_Service($client);
    if (isset($_GET['logout']))
    {
        unset($_SESSION['token']);
        die('Logged out.');
    }
    if (isset($_GET['code']))
    {
        $client->authenticate($_GET['code']);
        $_SESSION['token'] = $client->getAccessToken();
    }
    if (isset($_SESSION['token']))
    {
        $token = $_SESSION['token'];
        $client->setAccessToken($token);
    }
    if (!$client->getAccessToken())
    {
        $authUrl = $client->createAuthUrl();
        header("Location: ".$authUrl);
        die;
    }
    echo "<pre>";
    print_r($_SESSION);
    echo "</pre>";    
    echo 'Hello, world.';
?>

答案 2 :(得分:0)

该类曾经存在并且仍然可以粘贴副本。

立即使用Google_Service_Analytics。

答案 3 :(得分:0)

版本^ 2.0 中使用

// Use the developers console and download your service account
// credentials in JSON format. Place them in this directory or
// change the key file location if necessary.
$KEY_FILE_LOCATION = __DIR__ . '/service-account-credentials.json';

// Create and configure a new client object.
$client = new Google_Client();
$client->setApplicationName("Hello Analytics Reporting");
$client->setAuthConfig($KEY_FILE_LOCATION);
$client->setScopes(['https://www.googleapis.com/auth/analytics.readonly']);
$analytics = new Google_Service_AnalyticsReporting($client);

...