像其他人一样,我正在尝试让Google刷新令牌工作,以便运行复制和重命名文件的计划任务。
当我第一次在终端中手动认证时,我的url以&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发布于 2015-10-02 20:21:58
我曾经在那里工作过,下面的工作对我来说也是如此。
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
gauth = GoogleAuth()
gauth.LocalWebserverAuth()
gauth.LoadCredentialsFile(gauth)用上面的代码在根目录中创建一个新文件夹作为quickstart.py。
从google drive api credentials下载client_sercrets.json并将其放入根目录
将settings.yaml文件放在根文件夹中
从你的控制台运行quickstart.py,它会打开一个浏览器,要求你对应用程序进行授权。
此过程完成后,将在您的根目录中创建一个credentials.json文件。
访问令牌现在应该能够自我刷新。
Drive API设置需要注意的几点是,它应该是Web应用程序类型,在Authorize Javascript "http://localhost:8080“和授权重定向URI "http://localhost:8080/”中
如果成功,请将"credentials.json“、"client_secrets.json”、"settings.yaml“文件带到您的生产根目录中,它应该可以工作。
希望这能有所帮助!
https://stackoverflow.com/questions/32863213
复制相似问题