PyDrive guath.Refresh()和刷新令牌问题 [英] PyDrive guath.Refresh() and Refresh Token Issues
问题描述
与其他人一样,我正在尝试使Google刷新令牌起作用,以便运行复制和重命名文件的计划任务.
Like others, I'm trying to get the Google refresh token to work in order to run scheduled tasks that copy and rename files.
当我第一次在终端中手动进行身份验证时,我的网址以& access_type = offline结尾.但是,当我尝试在ipython中手动使用gauth.Refresh()时,它以与我的凭据文件过期时相同的错误失败:
When I first manually authenticate within a terminal, my url ends in &access_type=offline. However, when I go in and try to manually use gauth.Refresh() in ipython, it fails with the same error as when my credentials file expires:
pydrive.auth.RefreshError: No refresh_token found.Please set access_type of OAuth to offline.
实际上如何将access_type设置为脱机?任何建议都将不胜感激.
How do I actually set access_type to offline? Any suggestions are greatly appreciated.
I have been here, here, and here trying to troubleshoot this.
我的脚本:
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文件:
My settings.yaml file:
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
推荐答案
我去过那里,下面为我工作.
I have been there and below works for me.
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并将其放在根目录中
将您的settings.yaml文件放在根文件夹中
从控制台运行quickstart.py,它将打开一个浏览器,要求您对应用进行授权.
一旦完成此过程,它将在您的根目录中创建一个certificate.json文件.
访问令牌现在应该可以刷新自身.
Create a new folder with above code as quickstart.py in root dir.
Download client_sercrets.json from google drive api credentials and put it in root
Put your settings.yaml file in root folder
Run the quickstart.py from your console and it will open a browser ask you to authorise app.
Once this process is completed, it will create a credentials.json file in your root directory.
Access token should be able to refresh itself now.
关于Drive API设置的几点注意事项,应为Web应用程序类型,在授权Javascript"中< http://localhost:8080 "和授权重定向URI中的" http://localhost:8080/"
Few things to note for your Drive API settings, it should be Web application type, in Authorize Javascript "http://localhost:8080" and in Authorized redirect URIs "http://localhost:8080/"
如果成功,则将"credentials.json","client_secrets.json","settings.yaml"文件转移到生产根目录,并且应该可以使用.
If this is successful, carry over "credentials.json", "client_secrets.json", "settings.yaml" files to your production root dir and it should work.
希望这会有所帮助!
这篇关于PyDrive guath.Refresh()和刷新令牌问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!