0

我正在编写 PHP 脚本,该脚本从我的 Google Drive 文件夹中获取文件,并在我的网站上使用下载链接编写它们的列表,但找不到从我的文件夹中获取文件的解决方案。我发现的每个解决方案都需要用户注册,即查看页面并从他的谷歌驱动器中获取文件列表。谁能帮我解决问题?

这是我的代码。

function printFilesInFolder($service, $folderId) {
  $pageToken = NULL;
  do {
    try {
      $parameters = array();
       $parameters = array(
        'q' => "'0B5NCdsbrL1VfQ00xY3pFS3BtOE0' in parents"
        );     
  $children = $service->files->listFiles($parameters);
  var_dump($children);
  foreach ($children->getFiles() as $child) {
    print 'File Id: ' . $child->getId();
  }
  $pageToken = $children->getNextPageToken();
} catch (Exception $e) {
  print "An error occurred: " . $e->getMessage();
  $pageToken = NULL;
}
  } while ($pageToken);
}

$client = new Google_Client();
$client->setApplicationName("Web client 1");
$client->setClientId('CLIENTID');
$client->setClientSecret('SECRET');
$client->setScopes(array('https://www.googleapis.com/auth/drive.file'));
$client->setRedirectUri('http://localhost/googledrive/drive/drive.php');

if (isset($_GET['code']) || (isset($_SESSION['access_token']) && $_SESSION['access_token'])) {
if (isset($_GET['code'])) {
    $client->authenticate($_GET['code']);
    $_SESSION['access_token'] = $client->getAccessToken();
} else
    $client->setAccessToken($_SESSION['access_token']);
$service = new Google_Service_Drive($client);
$ret = printFilesInFolder($service,"FOLDERID");
} else {
    $authUrl = $client->createAuthUrl();
    header('Location: ' . $authUrl);
    exit();
}

但我仍然没有得到正确的数据。

第二个想法是,我想在每次刷新页面时都不需要身份验证,但我不知道该怎么做。

4

0 回答 0