I am testing my script that uses PyDrive to upload to a folder inside a shared drive. As it is, the files upload with no problem, however, the sharable link doesn't work. My program hits my exception block by then. I don't receive any errors.
file1 = drive.CreateFile(metadata={"title": "{0}_Index1.html".format(formatDate), 'mimeType':'text/html', "parents": [{ "kind": 'drive#fileLink', 'myteamDriveId': sharedDriveId, "id": myIdGoesHere }]})
file1.SetContentFile(fileName) # Set content of the file from given string.
file1.Upload(param={'supportsTeamDrives': True})
**# The script prints all output to here no issues. From the code below, it stops working.**
permission = file1.InsertPermission({
'type': 'anyone',
'value': 'anyone',
'role': 'reader'})
# SHARABLE LINK
link = file1['alternateLink']
Any suggestions?
Update: done some digging and this is the error:
<HttpError 404 when requesting https://www.googleapis.co... (continues... it's a json file, contents posted below)
returned "File not found: fileIDhere (continues.. some sort of file id?)
Lastly, here is the json output from the link in the error:
{
"error": {
"errors": [
{
"domain": "global",
"reason": "required",
"message": "Login Required",
"locationType": "header",
"location": "Authorization"
}
],
"code": 401,
"message": "Login Required"
}
}
In your situation, I understood that your file is put to the shared Drive. In this case, In order to create the permission, it is required to create the file in the shared Drive. For this, supportsAllDrives=True
is required to be included in the query parameter for requesting to "Permissions: insert". But when I saw the script of pydrive InsertPermission()
, permission = self.auth.service.permissions().insert(fileId=file_id, body=new_permission).execute(http=self.http)
is used for requesting. Ref In this case, this request cannot be used for the file in the shared Drive. I thought that by this, an error like File not found
occurred.
In order to avoid this issue, in the current stage, it seems that it is required to create the request using Drive API. The modified script is as follows.
file1 = drive.CreateFile(metadata={"title": "{0}_Index1.html".format(formatDate), 'mimeType': 'text/html', "parents": [{"kind": 'drive#fileLink', 'myteamDriveId': sharedDriveId, "id": myIdGoesHere}]})
file1.SetContentFile(fileName) # Set content of the file from given string.
file1.Upload(param={'supportsTeamDrives': True})
access_token = gauth.credentials.access_token # gauth is from drive = GoogleDrive(gauth) Please modify this for your actual script.
file_id = file1['id']
url = 'https://www.googleapis.com/drive/v3/files/' + file_id + '/permissions?supportsAllDrives=true'
headers = {'Authorization': 'Bearer ' + access_token, 'Content-Type': 'application/json'}
payload = {'type': 'anyone', 'value': 'anyone', 'role': 'reader'}
res = requests.post(url, data=json.dumps(payload), headers=headers)
# SHARABLE LINK
link = file1['alternateLink']
import requests
and import json
are used.