2021年3月28日星期日

Google Drive API v3 Python how to add parent to file that doesn't have a parent?

We are trying to move files into folders in Google Drive with the Google Drive API v3 in Python. This is our function for doing that.

def moveFiles(file_id, folder_id):      # Retrieve the existing parents to removed      file = service.files().get(fileId=file_id, fields='id, parents, name').execute()      # print(f"{file.get('name')} - {file.get('id')} - {folder_id} - {file.get('parents')}")                            if file.get('parents') == None:           service.files().update(fileId=file_id,                                      addParents=folder_id,                                      fields='id, parents',).execute()                                else:          if len(file.get('parents')) == 1:              past_parents = file.get('parents')[0]          else:              past_parents = ",".join(file.get('parents'))          fileservice = service.files().update(fileId=file_id,                                  removeParents = past_parents,                                  addParents=folder_id,                                  fields='id, parents').execute()  

The problem we're getting is that it's saying that we can't increase the number of parents. It works fine for folders with parents but for folders without parents it gives an error: HttpError(resp, content, uri=self.uri) googleapiclient.errors.HttpError: <HttpError 403 when requesting https://www.googleapis.com/drive/v3/files/19hmrfcPe37pX9CGh_ZIiIkzmrmhhGJvZddRO8bBRBO8?addParents=1DbiaUPSQYT8p-3Sc_yeH0t2jQF9GY9ig&fields=id%2C+parents&alt=json returned "Increasing the number of parents is not all owed". Details: "Increasing the number of parents is not allowed">

How can we add a parent to a file without a parent in Google Drive API v3 in Python?

https://stackoverflow.com/questions/66847908/google-drive-api-v3-python-how-to-add-parent-to-file-that-doesnt-have-a-parent March 29, 2021 at 10:02AM

没有评论:

发表评论