与其他人一样,我试图让Google刷新令牌工作,以便运行复制和重命名文件的计划任务。
当我第一次在终端内手动验证时,我的网址以& access_type = offline结尾。但是,当我进入并尝试在ipython中手动使用gauth.Refresh()时,它失败并出现与我的凭证文件到期时相同的错误:
pydrive.auth.RefreshError: No refresh_token found.Please set access_type of OAuth to offline.
如何将access_type实际设置为离线?非常感谢任何建议。
我的剧本:
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
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")
drive = GoogleDrive(gauth)
我的settings.yaml文件:
client_config_backend: settings
client_config:
client_id: ###actual client_id###
client_secret: ###actual 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
答案 0 :(得分:0)
我去过那里,下面为我工作。
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
gauth = GoogleAuth()
gauth.LocalWebserverAuth()
gauth.LoadCredentialsFile(gauth)
在根目录中创建一个上面代码为quickstart.py的新文件夹 从google drive api凭据下载client_sercrets.json并将其放在root中 将settings.yaml文件放在根文件夹中 从您的控制台运行quickstart.py,它将打开浏览器,要求您授权应用程序 完成此过程后,它将在根目录中创建credentials.json文件 访问令牌现在应该能够刷新自己。
您的云端硬盘API设置很少需要注意,它应该是Web应用程序类型,在授权Javascript" http://localhost:8080"以及授权重定向URI" http://localhost:8080/"
如果成功,请继续" credentials.json"," client_secrets.json"," settings.yaml"文件到你的生产根目录,它应该工作。
希望这有帮助!