【发布时间】:2021-03-31 17:01:44
【问题描述】:
我正在编写一个 Python 脚本来自动将一些文件上传到 Google Drive。由于我仍然是一名 Python 程序员新手,而且这与其他任何事情一样多,所以我开始关注Google Quickstart 并决定使用他们的quickstart.py 作为我自己脚本的基础。在讨论如何为 Python 脚本创建凭据的部分中,它指的是“创建凭据”链接,位于 https://developers.google.com/workspace/guides/create-credentials
我点击链接,进入我的一个 Google Cloud 项目,并尝试使用“内部”项目设置 OAuth 同意屏幕,正如他们告诉你的那样……但我做不到。谷歌说:
“由于您不是 Google Workspace 用户,因此您只能将您的 可供外部(普通观众)用户使用的应用程序。 ”
所以我尝试创建一个“外部”项目,然后继续使用桌面应用程序创建一个新的客户端 ID。然后我下载 JSON 凭据并将它们放在与我的 Python 脚本相同的文件夹中,即"credentials.json"。然后我执行 Python 脚本以对其进行身份验证:浏览器打开,我登录我的 Google 帐户,给它我的权限......然后浏览器挂起,因为它正在重定向到 localhost URL,显然我的小 Python 脚本是根本不听我的电脑。
我相信他们最近一定改变了这一点,因为一年前我开始遵循相同的 Python 教程并且可以毫无问题地创建凭据,但 Google Drive API 文档尚未更新。那么...我现在如何为 Python 脚本创建凭据?
编辑:在此处添加我的脚本的源代码。正如我所说,它与 Google 的“quickstart.py”非常相似:
from __future__ import print_function
import pickle
import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from googleapiclient.errors import HttpError
# If modifying these scopes, delete the file token.pickle.
SCOPES = ['https://www.googleapis.com/auth/drive.metadata', 'https://www.googleapis.com/auth/drive']
def main():
"""Shows basic usage of the Drive v3 API.
Prints the names and ids of the first 10 files the user has access to.
"""
creds = None
# The file token.pickle stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists('token_myappname.pickle'):
with open('token_myappname.pickle', 'rb') as token:
creds = pickle.load(token)
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
'credentials.json', SCOPES)
creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open('token_myappname.pickle', 'wb') as token:
pickle.dump(creds, token)
service = build('drive', 'v3', credentials=creds)
# Call the Drive v3 API
results = service.files().list(
pageSize=10, fields="nextPageToken, files(id, name)").execute()
items = results.get('files', [])
if not items:
print('No files found.')
else:
#print(items[0])
print('Files:')
for item in items:
#print (item)
print(u'{0} {1} {2}'.format(item['name'], item['owners'], item['parents']))
【问题讨论】:
-
您是否通过了应用验证流程?当您使用 Google Drive 时,我相信您的应用程序可能会使用以下 scopes 中的一些要求您的应用程序通过验证过程。
-
我没有。我的应用程序现在正处于“测试”阶段。但是,这在创建凭证时会有所不同吗?
-
这个脚本是否会自动运行(在后台,例如由调度程序运行,没有连接的用户)?脚本是否始终访问同一个 Google Drive?另外,你能分享一下你与 Drive API 建立连接的代码吗?
-
@guillaumeblaquiere:是的,我们的想法是它最终将作为 cron 作业运行,始终将文件上传到同一个驱动器。我已经添加了源代码,尽管它与 Google 教程的快速入门几乎相同。
标签: python google-cloud-platform google-drive-api google-oauth