CrazyHorse
CrazyHorse

Reputation: 53

Access "Shared with me folder" in Google Drive via Python

This is the problem I have to face, I'll be as clear as possible.

My clients are sending daily reports of their sales via an excel file on google Drive. I am only a viewer on this file and I have it in my "shared with me" folder in Google Drive. Ideally, I would like to import this file on Python to be able to post-process it and analyze it. Few notes:

  1. After adding it I see a .gsheet file that I do not know how to load in Python
  2. I am not sure that the file would be dynamically updated on daily basis

Let me know if you have other ideas/approaches! Thanks

Upvotes: 2

Views: 1980

Answers (1)

Juan Serrano
Juan Serrano

Reputation: 379

You can list all the files shared with you using the Drive API.

We will need to use the following methods:

Files.list [Drive API] (https://developers.google.com/drive/api/v3/reference/files/list) to list all files you have access to.

You can use the API explorer available in most documentation files and once you have a better grasp on the API behaviour experiment starting with this code sample https://developers.google.com/drive/api/quickstart/python, this Quickstart makes a simple list of files with Python.

I recommend you use the following flow: Call the Files.list method with the following parameters:

{
  "q": "not ('me' in owners or creator = 'me')",
  "fields": "nextPageToken,items(fileSize,owners,title,id,mimeType)"
}

This will return only the files you have opened that are shared with you (file you are not owner nor creator). For you to access .gsheet file you will not handle it as a regular file because they are not, instead use the Google Sheets API (https://developers.google.com/sheets/api/reference/rest) to fetch the data inside the Google Sheet file, the same thing is true for Google Docs and Google Slides, each have their respective API you can use to manipulate/access the data in each file.

If you look closely the parameters we are using, q filters the results you will obtain to only list files you don't own but can access, you can also filter files owned by a particular email address; the other parameter fields makes the response you obtain much shorter, since you won't make use of all the properties of a file this parameters provides a more simplifies response that will take less time for the server to process and less bandwidth, adjust the fields parameter if you need more or less data.

Finally, direct your focus to the nextPageToken property in the fields parameter, the API response will be paginated, meaning that you will receive up to a certain amount of files in one response, to retrieve the 'next page' of results just make the same call again but using the nextPageToken you obtained in the response as a new parameter in the request. This is explained in this documentation article https://developers.google.com/calendar/api/guides/pagination.

Note: If you need clarification on how to execute certain actions on a Google Sheet file I recommend you submit a new question since additional tasks with other APIs are outside the scope of this question and will make this response much larger than it needs to be.

Upvotes: 1

Related Questions