Shabutheen Basha
Shabutheen Basha

Reputation: 53

PyDrive Create folder in Google Drive - Error as AttributeError: 'GoogleDrive' object has no attribute 'files

Trying to create a folder in an existing folder using PyDrive based on dates but I keep on receiving the error as 'GoogleDrive' object has no attribute 'files' Documentation

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

gauth = GoogleAuth()
gauth.LoadCredentialsFile("token.txt")

if gauth.credentials is None:
    gauth.GetFlow()
    gauth.flow.params.update({'access_type': 'offline'})
    gauth.flow.params.update({'approval_prompt': 'force'})
    gauth.LocalWebserverAuth()
elif gauth.access_token_expired:
    gauth.Refresh()
else:
    gauth.Authorize()
gauth.SaveCredentialsFile("token.txt")  
drive = GoogleDrive(gauth)

file_metadata = {'name': 'Test','mimeType': 'application/vnd.google-apps.folder'}
file = drive.files().create(body=file_metadata).execute()
print('Folder ID: %s' % file.get('id'))

But I am receiving the error as "GoogleDrive' object has no attribute 'files'"

Is there anything I am doing wrong??

Upvotes: 4

Views: 1221

Answers (1)

vvvvv
vvvvv

Reputation: 31780

What worked in my case was using a different way of authenticating:

from httplib2 import Http
from googleapiclient.discovery import build
from oauth2client.service_account import ServiceAccountCredentials

SCOPES = ["https://www.googleapis.com/auth/drive"]
PATH_TO_SERVICE_KEYS = "/path/to/service/keys.json"
credentials = ServiceAccountCredentials.from_json_keyfile_name(PATH_TO_SERVICE_KEYS, scopes=SCOPES)
http_auth = credentials.authorize(Http())
drive = build("drive", "v3", http=http_auth)

This time, it worked without this error.

drive = GoogleDrive(gauth)

file_metadata = {"name": "Test", "mimeType": "application/vnd.google-apps.folder"}
file = drive.files().create(body=file_metadata).execute()

Upvotes: 1

Related Questions