-1

我正在尝试使用谷歌驱动器API函数retrieveAllFiles()从谷歌驱动器帐户显示文件列表,但由于它的参数,我不知道如何调用它。这是功能。

      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();
          print $pageToken;
        } catch (Exception $e) {
          print "An error occurred: " . $e->getMessage();
          $pageToken = NULL;
        }
      } while ($pageToken);
      return $result;
    }

我正在使用此链接作为参考: Files:list - Google Drive

我怎样才能得到谷歌驱动API的服务让我通过呢?

4

2 回答 2

0
<?php
require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_DriveService.php';

$client = new Google_Client();
// Get your credentials from the console
$client->setClientId('YOUR_CLIENT_ID');
$client->setClientSecret('YOUR_APP_SECRET');
$client->setRedirectUri('YOUR_REDIRECT_URI');
$client->setScopes(array('https://www.googleapis.com/auth/drive'));

$service = new Google_DriveService($client);

$authUrl = $client->createAuthUrl();

//Request authorization
print "Please visit:\n$authUrl\n\n";
print "Please enter the auth code:\n";
$authCode = trim(fgets(STDIN));

// Exchange authorization code for access token
$accessToken = $client->authenticate($authCode);
$client->setAccessToken($accessToken);

//Insert a file
$file = new Google_DriveFile();
$file->setTitle('My document');
$file->setDescription('A test document');
$file->setMimeType('text/plain');

$data = file_get_contents('document.txt'); //Put this file into same folder

$createdFile = $service->files->insert($file, array(
      //'data' => $data,
      'mimeType' => 'text/plain',
    )); ////Upload File
echo '<pre>';
//print_r($createdFile);
echo '</pre>';


//////Function for getting all files from your drive

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['items']); 

              $pageToken = $files['nextPageToken'];
            } catch (Exception $e) {
              print "An error occurred: " . $e->getMessage();
              $pageToken = NULL;
            }
        } while ($pageToken);           
        return $result;
    }   

$listss = retrieveAllFiles($service);
echo '<pre>';
print_r($listss);
echo '</pre>';
?>
于 2013-11-23T12:03:50.067 回答
0

$service指有Google_DriveService它是一个扩展Google_ServiceResource

要使用示例脚本,您需要下载完整的google-api-php-client

session_start();
require_once 'PATH_TO/src/Google_Client.php';
require_once 'PATH_TO/src/service/Google_ServiceResource.php';
require_once 'PATH_TO/src/contrib/Google_DriveService.php';


$client = new Google_Client();
$client->setApplicationName("Google Analytics PHP Starter Application");

$client->setClientId('insert_your_oauth2_client_id');
$client->setClientSecret('insert_your_oauth2_client_secret');
$client->setRedirectUri('insert_your_oauth2_redirect_uri');
$client->setDeveloperKey('insert_your_developer_key');
$service = new Google_DriveService($client);

点击这里下载

于 2012-10-26T13:07:41.017 回答