簡體   English   中英

在Google Drive API PHP中復制具有權限的文件

[英]Copy file with Permissions in Google Drive API PHP

我正在嘗試使用服務帳戶復制文件,然后授予對我的個人帳戶的訪問權限。 該副本似乎工作正常,似乎正在將文件復制到Google Drive服務帳戶。 因此,它正在返回創建的ID,但是在嘗試在文件上插入權限時失敗。 說未定義的方法插入。

這就是我現在所擁有的

private function copy_base_file( $new_file_name )
{
    $service = $this->get_google_service_drive( $this->get_google_client() );
    $origin_file_id = "{id of file to copy}";
    $copiedFile = new Google_Service_Drive_DriveFile();
    $copiedFile->setName($new_file_name);
    try {
        $response = $service->files->copy($origin_file_id, $copiedFile);
        $ownerPermission = new Google_Service_Drive_Permission();
        $ownerPermission->setEmailAddress("{myemailhere}");
        $ownerPermission->setType('user');
        $ownerPermission->setRole('owner');
        $service->permissions->insert("{sheet_id_here}", $ownerPermission, 
            ['emailMessage' => 'You added a file to ' .
            static::$applicationName . ': ' . "Does this work"]);
    } catch (Exception $e) {
        print "An error occurred: " . $e->getMessage();
    }
}

在Google雲端硬盤API的最新版本(V3)中不推薦使用insert方法。

請改用create方法。

V3驅動器API權限創建

private function copy_base_file( $new_file_name )
{
$service = $this->get_google_service_drive( $this->get_google_client() );
$origin_file_id = "{id of file to copy}";
$copiedFile = new Google_Service_Drive_DriveFile();
$copiedFile->setName($new_file_name);
try {
    $response = $service->files->copy($origin_file_id, $copiedFile);
    $ownerPermission = new Google_Service_Drive_Permission();
    $ownerPermission->setEmailAddress("{myemailhere}");
    $ownerPermission->setType('user');
    $ownerPermission->setRole('owner');
    $service->permissions->create("{sheet_id_here}", $ownerPermission, 
        ['emailMessage' => 'You added a file to ' .
        static::$applicationName . ': ' . "Does this work"]);
} catch (Exception $e) {
    print "An error occurred: " . $e->getMessage();
}

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM