iterative / PyDrive2

Google Drive API Python wrapper library. Maintained fork of PyDrive.
https://docs.iterative.ai/PyDrive2
Other
581 stars 69 forks source link

Shared Drive List #185

Closed FloatingMind12 closed 2 years ago

FloatingMind12 commented 2 years ago

I tried looking for a way to get the list of shared drives a user is member of because I don't want them to manually open a browser and copy the IDs themselves.

After some research, I found it in the official google drive API reference but what is the equivalent in pydrive? I looked through the documentation but seems like it's not there.

shcheklein commented 2 years ago

Yes, I don't think PyDrive provides an interface for that.

FloatingMind12 commented 2 years ago

So can you please tell me how to get the oauth key from pydrive so that I could make an API call with requests instead.

shcheklein commented 2 years ago

To get credentials, you could probably do:

gauth = GoogleAuth()
gauth.LocalWebserverAuth()
gauth.credentials

that's how more or less APIs are implemented:

from googleapiclient.discovery import build

http = httplib2.Http(timeout=self.http_timeout)
http = credentials.authorize(http)
service = build(
            "drive", "v2", http=self.http, cache_discovery=False
        )

metadata = (
                    service.files()
                    .get(
                        fileId=file_id,
                        fields=fields,
                        # Teamdrive support
                        supportsAllDrives=True,
                    )
                    .execute(http=self.http)
                )

I would try to execute service.drives() for example.

iva2k commented 2 years ago

Here's a more complete code example to get a list of Shared Drives:

from pydrive2.auth import GoogleAuth
from googleapiclient.discovery import build
import httplib2

gauth = GoogleAuth()
gauth.LocalWebserverAuth()
credentials = gauth.credentials
http_timeout = 10
http = httplib2.Http(timeout=http_timeout)
http = credentials.authorize(http)
service = build("drive", "v2", http=http, cache_discovery=False)
metadata = (
    service.drives()
    .list()
    .execute(http=http)
)
print(f'metadata={metadata}')
#  metadata={'nextPageToken': '<very-long-token>', 'kind': 'drive#driveList', 'items': [{'id': '1234ASDFG...', 'name': '1st Drive', 'kind': 'drive#drive'}, ...]}
for i,item in enumerate(metadata["items"]):
  print(f'  {i}: id={item["id"]} name="{item["name"]}"')

I suspect that if the list is very long, it needs to be requested in chunks using metadata['nextPageToken'] somehow.