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.