4

I am trying to download file from google drive with pydrive. code is modified from Automating pydrive verification process

And it is giving following error.

$ python driveDownload.py a a.xls
Google Drive Token Expired, Refreshing
Traceback (most recent call last):
  File "driveDownload.py", line 26, in <module>
    gauth.Refresh()
  File "build/bdist.linux-x86_64/egg/pydrive/auth.py", line 369, in Refresh
pydrive.auth.RefreshError: No refresh_token found.Please set access_type of OAuth to offline.

Hence I followed instructions as per stackoverflow here

Unable to get it working. Am I missing something basic ?

my settings.yaml is below

client_config_backend: settings 
client_config:   
  client_id: ###
  client_secret: ###

save_credentials: True 
save_credentials_backend: file 
save_credentials_file: credentials.json

get_refresh_token: True

oauth_scope:
  - https://www.googleapis.com/auth/drive.file
  - https://www.googleapis.com/auth/drive.install

I am getting following error :

$ python driveDownload.py a a.xls
Google Drive Token Expired, Refreshing
Traceback (most recent call last):
  File "driveDownload.py", line 26, in <module>
    gauth.Refresh()
  File "build/bdist.linux-x86_64/egg/pydrive/auth.py", line 369, in Refresh
pydrive.auth.RefreshError: No refresh_token found.Please set access_type of OAuth to offline.

my code is driveDownload.py :

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

#USAGE: python driveDownload.py googleDriveName localFileName
#authentication via the browser
#note... client_secrets.json is required for this that leads
#to the user authentication
fileName2Download = sys.argv[1]    
#myLocalPath is hard coded and 
myLocalPath = sys.argv[2]


# gauth = GoogleAuth()

gauth = GoogleAuth()
# Try to load saved client credentials
gauth.LoadCredentialsFile("GoogleDriveCredentials.txt")
if gauth.credentials is None:
    # Authenticate if they're not there
    gauth.LocalWebserverAuth()
elif gauth.access_token_expired:
    # Refresh them if expired
    print "Google Drive Token Expired, Refreshing"
    gauth.Refresh()
else:
    # Initialize the saved creds
    gauth.Authorize()
# Save the current credentials to a file
gauth.SaveCredentialsFile("GoogleDriveCredentials.txt")

#accessing the drive
drive = GoogleDrive(gauth)


# Auto-iterate through all files that matches this query
file_list = drive.ListFile().GetList()
for file1 in file_list:
#download the file hello.txt ad myDownload.txt
    print 'title: %s, id: %s , mimeType = %s' % (file1['title'], file1['id'],file1['mimeType'])
    if file1['title'] == fileName2Download:
        print ' file Title identiefied :%s'% (file1['title']) 
#         contentString = file1.GetContentString()
#         print 'contentString : %s' % (contentString)
        myFile = drive.CreateFile({'id': file1['id']})
        if os.path.exists(myLocalPath):
            os.remove(myLocalPath)

        myFile.GetContentFile(myLocalPath,mimetype='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
        print '%s downloaded ' %(myLocalPath)
        break
Community
  • 1
  • 1
idom
  • 51
  • 6
  • "No refresh_token found.Please set access_type of OAuth to offline." you need to fix your authentication. In order for your code to re-authenticate itself it needs a refresh token. The error message tells you what you need to do. sorry I cant help with python – Linda Lawton - DaImTo Jul 03 '15 at 08:07
  • @DalmTo Thanks for replying. I don't know how to do it. Any potential tip should help. If you can help with some other language that you are familiar, it will be greatly appreciated – idom Jul 03 '15 at 09:50
  • Maybe this example can help you, there is explained how to add parameters to the request, in this case offline access: https://developers.google.com/gmail/api/auth/web-server – Gerardo Jul 07 '15 at 00:31

0 Answers0