4

我正在尝试使用 pydrive 从谷歌驱动器下载文件。代码是从自动化 pydrive 验证过程中修改的

它给出了以下错误。

$ 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.

因此,我按照stackoverflow here的说明进行操作

无法让它工作。我错过了一些基本的东西吗?

我的 settings.yaml 在下面

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

我收到以下错误:

$ 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.

我的代码是 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
4

0 回答 0