首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PyDrive guath.Refresh()和刷新令牌问题

PyDrive guath.Refresh()和刷新令牌问题
EN

Stack Overflow用户
提问于 2015-09-30 18:00:24
回答 1查看 3.7K关注 0票数 6

像其他人一样,我正在尝试让Google刷新令牌工作,以便运行复制和重命名文件的计划任务。

当我第一次在终端中手动认证时,我的url以&access_type=offline结尾。但是,当我进入并尝试在ipython中手动使用gauth.Refresh()时,它失败了,出现的错误与我的凭据文件过期时的错误相同:

代码语言:javascript
复制
pydrive.auth.RefreshError: No refresh_token found.Please set access_type of OAuth to offline.

我如何将access_type设置为离线?我们非常感谢您的任何建议。

我一直在hereherehere上尝试解决这个问题。

我的脚本:

代码语言:javascript
复制
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文件:

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

发布于 2015-10-02 20:21:58

我曾经在那里工作过,下面的工作对我来说也是如此。

代码语言:javascript
复制
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“文件带到您的生产根目录中,它应该可以工作。

希望这能有所帮助!

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32863213

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档