Вывод всех файлов с Google Диска

Я хотел бы перечислить все файлы с моего диска Google в мой файл «DriveFiles.php», где я могу отображать файлы и их детали. Я новичок, поэтому вам будет полезен полный код. Спасибо.

Мой код:

<?php


require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_DriveService.php';
require_once 'google-api-php-client/src/io/Google_HttpRequest.php';
require_once 'google-api-php-client/src/contrib/Google_Oauth2Service.php';

// initialize a client with application credentials and required scopes.
$client = new Google_Client();
$client->setClientId('CLIENT_ID');
$client->setClientSecret('CLIENT_SECRET');
$client->setRedirectUri('REDIRECT_URI');
$client->setScopes(array(
          'https://www.googleapis.com/auth/drive',
          'https://www.googleapis.com/auth/userinfo.email',
          'https://www.googleapis.com/auth/userinfo.profile'));
$client->setUseObjects(true);


if (isset($_GET['code']))
   {
   session_start();
   print_r($_SESSION);
   $client->authenticate($_GET['code']);
   $_SESSION['token'] = $client->getAccessToken();
   $client->setAccessToken($_SESSION['token']);
   // initialize the drive service with the client.
   $services = new Google_DriveService($client);
   retrieveAllFiles($services);    

  }



 if(!$client->getAccessToken()){
   $authUrl = $client->createAuthUrl();
   echo '<a class="login" href="'.$authUrl.'">Login</a>';
   }


 function retrieveAllFiles($service) {
   $result = array();
   $pageToken = NULL;

          do {
            try {
              $parameters = array();
              if ($pageToken) {
                $parameters['pageToken'] = $pageToken;
              }
              $files = $service->files->listFiles($parameters);

              $result = array_merge($result, $files->getItems());
              $pageToken = $files->getNextPageToken();
            } catch (Exception $e) {
              print "An error occurred: " . $e->getMessage();
              $pageToken = NULL;
            }
          } while ($pageToken);
          return $result;
        }
        ?>

Когда я выполняю код, я получаю следующую ошибку:

Неустранимая ошибка: неперехваченное исключение 'Google_Exception' с сообщением 'Невозможно добавить службы после аутентификации' в D: \ GT_local \ Public \ google-api-php-client \ src \ Google_Client.php: 115 Трассировка стека: # 0 D: \ GT_local \ Public \ google-api-php-client \ src \ contrib \ Google_DriveService.php (1258): Google_Client-> addService ('диск', 'v2') # 1 D: \ GT_local \ Public \ quickstart.php (55) : Google_DriveService -> __ construct (Object (Google_Client)) # 2 {main} добавлено в "FILE_LOCATION (C: // google-api-php-client \ src \ Google_Client.php в строке 115)"

Как я могу это исправить.


person Vinny Pamnani    schedule 04.11.2014    source источник
comment
Я бы начал с использования новейшей клиентской библиотеки. Тот, который вы используете, больше не разрабатывается. github.com/google/google-api-php-client   -  person DaImTo    schedule 04.11.2014


Ответы (1)


Попробуйте запустить сеанс в верхней части сценария и попробуйте выполнить всю аутентификацию, прежде чем выполнять какие-либо операции. Также используйте самый последний API, чтобы вы могли использовать эти библиотеки:

require_once '/src/Google/autoload.php';
require_once '/src/Google/Client.php';
require_once '/src/Google/Service/Oauth2.php';
require_once '/src/Google/Service/Drive.php';
person Rg14    schedule 08.06.2015