【问题标题】:Composer and Php Google Api ClientComposer 和 Php Google Api 客户端
【发布时间】:2015-03-26 02:02:45
【问题描述】:

这是我的 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 的库?

非常感谢

【问题讨论】:

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


    【解决方案1】:

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

    $service = new Google_Service($client);
    

    【讨论】:

      【解决方案2】:

      我知道这很旧,但我没有看到任何答案,而且还没有足够的信息......将范围设置为 '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.';
      ?>
      

      【讨论】:

        【解决方案3】:

        该类曾经存在并且仍然被复制粘贴。

        立即使用 Google_Service_Analytics。

        【讨论】:

          【解决方案4】:

          版本^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);
          
          ...
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2019-12-10
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-09-23
            • 2016-12-03
            相关资源
            最近更新 更多