【问题标题】:Google Drive api setting folder parentsGoogle Drive api设置文件夹父母
【发布时间】:2015-02-04 10:13:08
【问题描述】:

我一直在尝试使用 google api 首先创建一个根文件夹,然后在该根文件夹中创建子文件夹。然而,结果并不令人鼓舞。下面是创建文件夹的代码:

public function createFolder($name, $parents = array())
    {    
        /** @var $client \Google_Client */
        $client = $this->client;
        /** @var $service \Google_Service_Drive */
        $service = $this->service;

        if (is_null($service)) {
            throw new Exception("Invalid google api service");
        }

        if (!$client instanceof \Google_Client) {
            throw new Exception("Invalid google api client");
        }

        if (!isset($_SESSION['access_token'])) {
            throw new Exception("No access token found");
        }

        if (!$client->getAccessToken()) {
            throw new Exception("Could not find access token in client");
        }

        $folder = new \Google_Service_Drive_DriveFile();
        $folder->setTitle($name);
        $folder->setMimeType('application/vnd.google-apps.folder');

        if (!empty($parents)) {
            echo "Setting parents of $name to: " . implode(',', $parents);
            $folder->setParents($parents);
        } else {
            echo "\n No parent ids found \n";
        }


        echo "\n Creating new folder: {$name} \n";

        $output = $service->files->insert($folder, array(
            'mimeType' => 'application/vnd.google-apps.folder',
        ));

        return $output;

    }

然而,每当我运行这个脚本时,父母都没有设置。相反,子文件夹位于父文件夹旁边的驱动器中。

我也尝试像这样手动设置父标志:

public function insertFileToFolder($childId, $parentId)
    {
        if (empty($childId) || empty($parentId)) {
            throw new Exception("Invalid parameter");
        }

        $service = $this->getService();

        try {

            echo "\n Trying to set subfolders \n";
            $parentReference = new \Google_Service_Drive_ParentReference();
            $parentReference->setId($parentId);

            return $service->parents->insert($childId, $parentReference);

        } catch (Exception $ex) {
            echo $ex->getMessage();
        }

        return null;
    }

哪个有效……有点。它不会将子文件夹“移动”到父文件夹,而是复制它们或创建某种符号链接,并且这些文件夹似乎位于父文件夹下。

编辑:我已经确认 insertFileToFolder 不会复制文件而是设置符号链接。从根目录删除文件夹也会将其从父文件夹中删除。

真正简单的问题:我错过了什么?

【问题讨论】:

    标签: php google-drive-api google-api-php-client


    【解决方案1】:

    在您的函数“insertFileToFolder”中,不要使用 parent->insert 函数,而是使用 children->insert('FolderID', 'newChild')

    你可以在这个链接上看到一个确切的实现:https://developers.google.com/drive/v2/reference/children/insert#examples

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-06
      • 1970-01-01
      相关资源
      最近更新 更多