0

I am trying to list the contents for google drive using python. I have an application running on GCP Cloud Run. This is the class that wraps the GDrive api.

class GDriveHelper:
    def __init__(self, credentials=None) -> None:
        if credentials:
            self.service = build("drive", "v3", credentials=credentials)
        else:
            self.service = build("drive", "v3")

    def list_files(self):
        results = (
            self.service.files()
            .list(
                pageSize=1000,
                fields="nextPageToken, files(id, name, mimeType, size, modifiedTime)",
            )
            .execute()
        )
        items = results.get("files", [])
        return items

Running local, I am able to get the files list, using snippet below:


scope = ["https://www.googleapis.com/auth/drive"]

service_account_json_key = "secret.json"
credentials = service_account.Credentials.from_service_account_file(
    filename=service_account_json_key, scopes=scope
)

gdrive_helper = GDriveHelper(credentials=credentials)
files = gdrive_helper.list_files()

In secret json file I am using "client_email": "cloud-build@primary-care-378721.iam.gserviceaccount.com" the same service account that the cloud run app was deployed.

The problem is when calling, this from Cloud Run app:

gdrive_helper = GDriveHelper()
files = gdrive_helper.list_files()

It's returning a empty list.

DominicT
  • 388
  • 1
  • 9
p.magalhaes
  • 7,595
  • 10
  • 53
  • 108
  • What does this mean from your post `In secret JSON file I am using ...`? Are you using the service account JSON key file that you downloaded from Google or something else you hand crafted? – John Hanley Jun 15 '23 at 20:10
  • I just got! I will update the question. The cloud run is using a different service-account, the default one. :) – p.magalhaes Jun 15 '23 at 22:15
  • 1
    Keep in mind that the list function list all the file in the Drive account personal directory (like My Drive on Google Drive). Most of the time, you don't want that, you want to access share with me drive or shared drive location. – guillaume blaquiere Jun 16 '23 at 07:17

0 Answers0