googlearchive / PyDrive

Google Drive API Python wrapper library
Other
1.31k stars 273 forks source link

I have issue to download from shared drive. #186

Open billchenJT opened 4 years ago

billchenJT commented 4 years ago

I am using python to fetch files from the google shared drive, which is owned by the team. The currently issue is I can parse all the file ID's but I cannot using the pyDrive to download or read the content of the file.

First I generated a "drive":

from pydrive.drive import GoogleDrive
from pydrive.auth import GoogleAuth
from pprint import pprint

gauth = GoogleAuth()
# Create local webserver and auto handles authentication.
gauth.LocalWebserverAuth()

drive = GoogleDrive(gauth)

Then I use the file id to get the content:

file_id = 'xxx'
file_ = drive.CreateFile({'id': file_id})
content = file_.GetContentString() # <--------------problem line
print(content)

The error I got is:

pydrive.files.ApiRequestError: <HttpError 404 when requesting https://www.googleapis.com/drive/v2/files/xxx?alt=json returned "File not found: xxx">

However, the file does exist when I go to the https://www.googleapis.com/drive/v2/files/xxx?alt=json, it return:

{
 "error": {
  "errors": [
   {
    "domain": "usageLimits",
    "reason": "dailyLimitExceededUnreg",
    "message": "Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup.",
    "extendedHelp": "https://code.google.com/apis/console"
   }
  ],
  "code": 403,
  "message": "Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup."
 }
}

I have no idea what is going on. Please help! Thanks,

AstronoTech commented 4 years ago

Hello billchenJT,

I had the same problem which was a bit annoying. It turns out it is a small modification to PyDrive lib itself. I just figured it out from another post and it works (thanks to SMB784):

https://github.com/gsuitedevs/PyDrive/issues/160#issuecomment-499920540

Once you have brought the modification to files.py in PyDrive, you can use the following code to download any file from the Team Drive (it worked for me):

from pydrive.auth import GoogleAuth from pydrive.drive import GoogleDrive

gauth = GoogleAuth() gauth.LoadClientConfigFile('client_secrets.json') drive = GoogleDrive(gauth)

team_drive_id = 'XXXXXXXXX' parent_folder_id = 'XXXXXXXXX' file_id = 'XXXXXXXXX'

f = drive.CreateFile({ 'id': file_id, 'parents': [{ 'kind': 'drive#fileLink', 'teamDriveId': team_drive_id, 'id': parent_folder_id }] })

f.GetContentFile('')

I hope it helps :) Kind regards, Berti

shcheklein commented 4 years ago

@Berti30 there is an actively maintained version PyDrive2 (Travis tests, regular releases including conda) from the DVC.org team. I think is solves this and some other problems - please give it a try and let us know - https://github.com/iterative/PyDrive2

cartochris commented 4 years ago

I have a similar problem with a 404 file not found message but using InsertPermission() with a file on a shared drive (it works fine on my drive).

PyDrive2 doesn't appear to change this and I am struggling to see what the issue might be (I have confirmed the file ID is correct as well).

Do additional 'supportsTeamDrives' entries need to be added to the permission functions as well?

cartochris commented 4 years ago

It appears as though that is the issue, I added the supportsTeamDrives parameter to InsertPermission() at line 325 of files.py and it has inserted the permission:

permission = self.auth.service.permissions().insert( fileId=file_id, body=new_permission, supportsTeamDrives=True).execute(http=self.http)

shcheklein commented 4 years ago

@cartochris would be great if you could create a PR to the actively maintained version - iterative/PyDrive2 ... we already fixed most of these issues with supportAllDrives (I think supportsTeamDrives was deprecated in favor of supportAllDrives)

cartochris commented 4 years ago

Sure just added a PR.