Search code examples
google-drive-apigoogle-api-php-clientphp-curl

How can I get share link from Google Drive REST api v3?


After uploading a file to GoogleDrive, I can get an fileId from the response, then I pass the fileId to the following function:

public function createShareLink($fileId, $accessToken){
    $ch = curl_init();
    $options = [
        CURLOPT_URL =>  'https://www.googleapis.com/drive/v3/files/'.$fileId.'/permissions',
        CURLOPT_POST => true,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_POSTFIELDS => json_encode(['type'=>'anyone', 'role'=>'reader',]),
        CURLOPT_HTTPHEADER => [
            'Authorization:Bearer '.$accessToken,
            'Content-Type:application/json',
        ],
        //In case you're in Windows, sometimes will throw error if not set SSL verification to false
        CURLOPT_SSL_VERIFYPEER => false,
        CURLOPT_SSL_VERIFYHOST => false,
    ];
    //In case you need a proxy
    //$options[CURLOPT_PROXY] = 'http://127.0.0.1:1087';

    curl_setopt_array($ch, $options);
    $result = curl_exec($ch);
    if (curl_errno($ch)) {
        echo 'Error:' . curl_error($ch);
    }
    curl_close ($ch);
    return $result;
}

It did works and it returns a json:

{
 "kind": "drive#permission",
 "id": "anyoneWithLink",
 "type": "anyone",
 "role": "reader",
 "allowFileDiscovery": false
}

The file in GoogleDrive have indeed become shared status: shared file in GoogleDrive

But there's no share link in the response json, so I check the documentation, in here, you can find the fields parameter(see the ScreenShot below): fields parameter

Click the partial response will redirect you to a page which includes some examples about how you pass values to the fileds parameters.

I follow the example pass webViewLink as value to fields like this:

CURLOPT_URL =>  'https://www.googleapis.com/drive/v3/files/'.$fileId.'/permissions?fields=webViewLink',

But response an error:

{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "invalidParameter",
    "message": "Invalid field selection webViewLink",
    "locationType": "parameter",
    "location": "fields"
   }
  ],
  "code": 400,
  "message": "Invalid field selection webViewLink"
 }
}

I tried id:

CURLOPT_URL =>  'https://www.googleapis.com/drive/v3/files/'.$fileId.'/permissions?fields=id',

The response is:

{
 "id": "anyoneWithLink"
}

I tried name:

CURLOPT_URL =>  'https://www.googleapis.com/drive/v3/files/'.$fileId.'/permissions?fields=name',

The response is:

{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "invalidParameter",
    "message": "Invalid field selection name",
    "locationType": "parameter",
    "location": "fields"
   }
  ],
  "code": 400,
  "message": "Invalid field selection name"
 }
}

I tried mimeType:

CURLOPT_URL =>  'https://www.googleapis.com/drive/v3/files/'.$fileId.'/permissions?fields=mimeType',

The response is:

{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "invalidParameter",
    "message": "Invalid field selection mimeType",
    "locationType": "parameter",
    "location": "fields"
   }
  ],
  "code": 400,
  "message": "Invalid field selection mimeType"
 }
}

I really don't know how can this fields parameter works, cause I think webViewLink, name and mimeType are correct fields, they all describe in here, anyone did this before? I'm not gonna use the google-api-php-client cause its size is too large(>20M).


Solution

  • Your requirements:

    • You want to retrieve webViewLink using php.
    • You want to achieve this using curl without using googleapis of php.
    • You have already been used Drive API.

    Issue:

    • In your case, you are trying to retrieve webViewLink from the method of Permissions: create in Drive API. Unfortunately, the method of Permissions: create has no fields of file. By this, the fields of webViewLink, name and mimeType cannot be directly retrieved from the method of Permissions: create.
    • About I really don't know how can this fields parameter works, cause I think webViewLink, name and mimeType are correct fields, they all describe in [here](https://developers.google.com/drive/api/v3/reference/files#resource-representations), anyone did this before?,
      • In this case, please check Permissions. You can see the fields parameters at there.

    By above sitaution, such error occurs.

    Workaround 1:

    Fortunately, the format of webViewLink is the constant. Using this, you can directly retrieve webViewLink using the file ID like below.

    $webViewLink = 'https://drive.google.com/file/d/'.$fileId.'/view?usp=drivesdk';
    

    Workaround 2:

    If you want to retrieve webViewLink using API, how about using the method of Files: get in Drive API? The sample script is as follows.

    Sample script:

    function getWebViewLink($fileId, $accessToken){
        $ch = curl_init();
        $options = [
            CURLOPT_URL =>  'https://www.googleapis.com/drive/v3/files/'.$fileId.'?fields=webViewLink',
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_HTTPHEADER => ['Authorization:Bearer '.$accessToken],
        ];
        curl_setopt_array($ch, $options);
        $result = curl_exec($ch);
        if (curl_errno($ch)) {
            echo 'Error:' . curl_error($ch);
        }
        curl_close ($ch);
        return $result;
    }
    

    Reference: