没有本地服务器的pydrive身份验证 - Python

时间:2015-10-26 16:40:29

标签: python google-drive-api

我有以下Python代码:

#!/usr/bin/env python
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive

gauth = GoogleAuth()
# Try to load saved client credentials
gauth.LoadCredentialsFile("mycreds.txt")
if gauth.credentials is None:
    # Authenticate if they're not there
    gauth.LocalWebserverAuth()
elif gauth.access_token_expired:
    # Refresh them if expired
    gauth.Refresh()
else:
    # Initialize the saved creds
    gauth.Authorize()
# Save the current credentials to a file
gauth.SaveCredentialsFile("mycreds.txt")

drive = GoogleDrive(gauth)

file1 = drive.CreateFile({'title': 'Hello3.txt'})
# Create GoogleDriveFile instance with title 'Hello.txt'
file1.SetContentString('Hello World!') # Set content of the file from given string
file1.Upload()

# Auto-iterate through all files that matches this query
file_list = drive.ListFile({'q': "'root' in parents and trashed=false"}).GetList()
for file1 in file_list:
      print 'title: %s, id: %s' % (file1['title'], file1['id'])

mycreds.txt的格式为:

client_id = "foobarbaz.apps.googleusercontent.com"
client_secret = "foobarbaz"

但是,每次执行该脚本时,本地服务器仍然会进行手动身份验证。我的目标是自动化该部分,因此用户无需单击授权按钮。 我究竟做错了什么?

1 个答案:

答案 0 :(得分:0)

授权访问云端硬盘时始终需要用户同意。避免授权将取消Google的安全政策。有关详细信息,请参阅此处:https://developers.google.com/apps-script/guides/services/authorization和此处:https://developers.google.com/drive/web/about-auth