簡體   English   中英

公開的Google Drive API共享鏈接

[英]Google drive API shareable link for public

我在Laravel 5.2中使用Google Drive API。 我可以授予文件訪問Gmail的特定電子郵件ID的權限。 但是,我想公開授予所有擁有文件鏈接的訪問權限。

我已經嘗試過該webViewLinkalternateLinkwebContentLink ,但是所有這些都是空白。

請參見文件對象的圖像輸出

在此處輸入圖片說明

    if(isset($options['name']) && isset($options['path'])){
        $auth_con = new AuthController();
        $client = $auth_con->getClient();
        $service = new \Google_Service_Drive($client);
        $fileMetadata = new \Google_Service_Drive_DriveFile(array(
            'name' => $options['name']));
        $content = file_get_contents($options['path']);
        $file = $service->files->create($fileMetadata, array(
            'data' => $content,
            'uploadType' => 'multipart',
            'fields' => 'id'
        ));
        $file_id = $file->id;
        if(isset($options['to'])){
            foreach ($options['to'] as $key => $value) {
                drive_permission($service, $value, $file_id);
            }
        }
     }


    function drive_permission($service, $email_id, $file_id)
    {
      $service->getClient()->setUseBatch(true);
      try {
        $batch = $service->createBatch();

        $userPermission = new Google_Service_Drive_Permission(array(
            'type' => 'group',
            'role' => 'reader',
            'emailAddress' => $email_id
        ));
        $request = $service->permissions->create(
            $file_id, $userPermission, array('fields' => 'id'));
        $batch->add($request, 'anyone');
        $results = $batch->execute();
      } finally {
        $service->getClient()->setUseBatch(false);
    }
}

對於Google雲端硬盤v2,請使用alternateLink屬性。 在Google Drive v3中,使用webViewLink屬性。

也根據此鏈接

在我使用PHP Api v3的情況下,要使鏈接為非空鏈接,必須定義您請求此字段,並且如果您具有正確的權限:

所以像這樣:

 $file =self::$service->files->get("1ogXyGxcJdMXt7nJddTpVqwd6_G8Hd5sUfq4b4cxvotest",array("fields"=>"webViewLink")); 

暫無
暫無

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

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