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:

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):

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).