首页 > 解决方案 > 如何在 php 中使用 Google 控制台服务帐户通过 Google Drive API 获取文件夹/文件列表

问题描述

我在过去的几天里正在搜索这个东西,以使用服务帐户获取子文件夹/文件列表没有任何方法可以请任何人帮助我如何获得它?

我使用了以下一些代码:-

define('FOLDER_ID', '1acTBwQK8QH5OERZYwuqSnt6sMDdf4V2c');

require_once './vendor/autoload.php';


putenv('GOOGLE_APPLICATION_CREDENTIALS=' . __DIR__ . '/service-account.json');

function getGoogleClient()
{
    return getServiceAccountClient();
}

function getServiceAccountClient()
{
    try {
        // Create and configure a new client object.        
        $client = new Google_Client();
        $client->useApplicationDefaultCredentials();
        $client->addScope(
            "https://www.googleapis.com/auth/drive",
            "https://www.googleapis.com/auth/drive.appfolder"
        );
        return $client;
    } catch (Exception $e) {
        print "An error occurred: " . $e->getMessage();
    }
}



$client = getGoogleClient();
$service = new Google_Service_Drive($client);
// $student_folder = $service->children->listChildren('1HmwwEabelrR_q5MQqkUWMrlYsTgtLDdr');

$optParams = array(
    'fields' => '*'
);
$results = $service->files->get(FOLDER_ID, $optParams);
$permissions = $results->getPermissions();

$files = $service->files->get(FOLDER_ID);

echo '<pre>';
print_r($files);
echo '</pre>';

它根据文件夹ID返回我的主文件夹名称或详细信息我将其设置为静态但我如何获取子文件夹/文件列表并创建文件夹和上传文件我很困惑

如果有任何其他方式 pelese 向我推荐了我如何设置它,如果有任何博客,那么还请添加一个链接以供参考。

我需要它在 PHP 中使用任何库。

感谢您的帮助。

标签: phpwordpress

解决方案


推荐阅读