use*_*030 5 oauth google-api google-api-python-client pydrive
像其他人一样,我试图让 Google 刷新令牌工作,以便运行复制和重命名文件的计划任务。
当我第一次在终端内手动验证时,我的 url 以 &access_type=offline 结尾。但是,当我进入并尝试在 ipython 中手动使用 gauth.Refresh() 时,它失败并出现与我的凭据文件过期时相同的错误:
pydrive.auth.RefreshError: No refresh_token found.Please set access_type of OAuth to offline.
Run Code Online (Sandbox Code Playgroud)
我如何实际将 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)
Run Code Online (Sandbox Code Playgroud)
我的 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
Run Code Online (Sandbox Code Playgroud)
小智 -1
我去过那里,下面对我有用。
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
gauth = GoogleAuth()
gauth.LocalWebserverAuth()
gauth.LoadCredentialsFile(gauth)
Run Code Online (Sandbox Code Playgroud)
在根目录中使用上述代码创建一个新文件夹,作为quickstart.py。
从 google Drive api 凭据下载 client_sercrets.json 并将其放入根
目录中 将 settings.yaml 文件放入根文件夹中
从控制台运行quickstart.py,它将打开一个浏览器,要求您授权应用程序。
此过程完成后,它将在您的根目录中创建一个credentials.json 文件。
访问令牌现在应该能够自行刷新。
Drive API 设置需要注意的事项很少,它应该是 Web 应用程序类型,在授权 Javascript 中“ http://localhost:8080 ” 和授权重定向 URI 中“ http://localhost:8080/ ”
如果成功,请将“credentials.json”、“client_secrets.json”、“settings.yaml”文件转移到您的生产根目录,它应该可以工作。
希望这可以帮助!
归档时间: |
|
查看次数: |
3304 次 |
最近记录: |