这是我的composer.json:
{
"require": {
"google/apiclient": "1.0.*@beta"
}
}
这是我的代码:
<?
$path = get_include_path() . PATH_SEPARATOR . 'C:wampwwwgCalendarvendorgoogleapiclientsrc';
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:wampwwwgCalendarindex.php on line 21
我做错了什么,包括Composer的库?
非常感谢
该库中不存在类Google_AnalyticsService
。请改用Google_Service
。
$service = new Google_Service($client);
我知道这是旧的,但我看不到答案,而且没有足够的答案。。。是否将范围设置为"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.';
?>
这个类曾经存在,但仍然可以复制粘贴。
现在使用Google_Service_Analytics。
在版本^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);
...