Ejemplo n.º 1
0
 public static function getServiceClient()
 {
     if (!self::isServiceConfigured()) {
         return false;
     }
     $config = self::getConfig();
     $client = new apiClient(array("ioFileCache_directory" => PIMCORE_CACHE_DIRECTORY));
     $client->setApplicationName("pimcore CMF");
     $key = file_get_contents(self::getPrivateKeyPath());
     $client->setAssertionCredentials(new apiAssertionCredentials($config->email, array('https://www.googleapis.com/auth/analytics.readonly', "https://www.google.com/webmasters/tools/feeds/"), $key));
     $client->setClientId($config->client_id);
     // token cache
     $tokenFile = PIMCORE_SYSTEM_TEMP_DIRECTORY . "/google-api.token";
     if (file_exists($tokenFile)) {
         $tokenData = file_get_contents($tokenFile);
         $tokenInfo = Zend_Json::decode($tokenData);
         if ($tokenInfo["created"] + $tokenInfo["expires_in"] > time() - 900) {
             $token = $tokenData;
         }
     }
     if (!$token) {
         $client->getAuth()->refreshTokenWithAssertion();
         $token = $client->getAuth()->getAccessToken();
         file_put_contents($tokenFile, $token);
     }
     $client->setAccessToken($token);
     return $client;
 }