如何使用谷歌驱动器服务检索文件夹

时间:2015-01-10 19:49:51

标签: php google-drive-api

我想要使用Google云端硬盘服务检索文件夹。 第一个问题是我不知道文件夹Id。我只知道驱动器中的文件夹名称。

第二个问题是我只找到这段代码用于下载文件,而不是文件夹https://developers.google.com/drive/web/manage-downloads

这是我的代码的开头,但它不起作用:(

session_start();

define( 'GDRIVE_CLIENT_ID', '422321818234bg877v326qtojhcsgsqr6p7fd563jmmh.apps.googleusercontent.com' ); 
define( 'GDRIVE_CLIENT_SECRET', '1V5ADb4CpMZZqdSHfsORWro1' ); 
define( 'GDRIVE_REDIRECT_URIS', 'http://localhost/drive.php' ); 

define( 'GDRIVE_SCOPE_01', 'https://www.googleapis.com/auth/drive' ); 
define( 'GDRIVE_SCOPE_02', 'https://www.googleapis.com/auth/drive.apps.readonly' ); 
define( 'GDRIVE_SCOPE_03', 'https://www.googleapis.com/auth/drive.file' ); 
define( 'GDRIVE_SCOPE_04', 'https://www.googleapis.com/auth/drive.metadata.readonly' ); 
define( 'GDRIVE_SCOPE_05', 'https://www.googleapis.com/auth/drive.readonly' ); 
define( 'GDRIVE_FILE_KEY', 'https://drive.google.com/folderview?id=0B_S8U3a8QeR_cUlCRTBLOWxiUnc&usp=sharing' ); 

$client = new Google_Client(); 
$client->setClientId( GDRIVE_CLIENT_ID ); 
$client->setClientSecret( GDRIVE_CLIENT_SECRET ); 
$client->setRedirectUri( GDRIVE_REDIRECT_URIS ); 
$client->setScopes( array( GDRIVE_SCOPE_01, GDRIVE_SCOPE_02, GDRIVE_SCOPE_03, GDRIVE_SCOPE_04, GDRIVE_SCOPE_05 ) ); 


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);
} else {
    $authUrl = $client->createAuthUrl();
    header('Location: ' . $authUrl);
    exit();
}

try { 
  $file = $service->files->get( GDRIVE_FILE_KEY ); 
  echo "Title: ", $file->getTitle(); 
  echo "Description: ", $file->getDescription(); 
  echo "MIME type: ", $file->getMimeType(); 
} catch (Exception $e) { 
  echo "An error occurred: ", $e->getMessage(); 
} 

1 个答案:

答案 0 :(得分:1)

“第一个问题是我不知道文件夹ID。我只知道我的驱动器中的文件夹名称。”

使用包含文件名的查询执行files.list。请参阅https://developers.google.com/drive/v2/reference/files/listhttps://developers.google.com/drive/web/search-parameters