我正在尝试使用pydrive从谷歌驱动器下载文件。代码已从Automating pydrive verification process
修改它正在发出以下错误。
$ 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