在代码执行并且浏览器显示错误消息时,尝试使用带有Drive API的Python以编程方式将文件上传到Google Drive,这会遇到身份验证问题。
背景和预期功能:
适用于Windows的Python桌面应用程序,允许用户在身份验证后将文件上传到其各自的Google云端硬盘帐户。怀疑这可能是由于范围定义引起的,但不是很确定,因为将范围从https://www.googleapis.com/auth/drive更改为较不敏感的内容,例如https://www.googleapis.com/auth/drive.file仍然显示相同的错误。
此代码基于Drive API官方文档和下面列出的其他来源。
代码参考:
视频教程参考:
https://www.youtube.com/watch?v=9OYYgJUAw-w
a)在执行首次身份验证代码时,浏览器显示错误消息:该应用未通过验证。我试图通过转到“高级”链接并手动设置配置访问权限来绕过此问题,从而导致浏览器消息“身份验证流程已完成”。
b)再次执行该应用程序时,执行的结果与步骤a)相同,而不是将文件上传到Google云端硬盘。
我看到已创建一个名为“ .credentials”的子文件夹,但未创建带有必需访问文件的.json。
auth.py
from __future__ import print_function
import httplib2
import os
from apiclient import discovery
from oauth2client import client
from oauth2client import tools
from oauth2client.file import Storage
try:
import argparse
flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
except ImportError:
flags = None
class auth:
def __init__(self,SCOPES,CLIENT_SECRET_FILE,APPLICATION_NAME):
self.SCOPES = SCOPES
self.CLIENT_SECRET_FILE = CLIENT_SECRET_FILE
self.APPLICATION_NAME = APPLICATION_NAME
def getCredentials(self):
"""Gets valid user credentials from storage.
If nothing has been stored, or if the stored credentials are invalid,
the OAuth2 flow is completed to obtain the new credentials.
Returns:
Credentials, the obtained credential.
"""
cwd_dir = os.getcwd()
credential_dir = os.path.join(cwd_dir, '.credentials')
if not os.path.exists(credential_dir):
os.makedirs(credential_dir)
credential_path = os.path.join(credential_dir, 'client_secret.json')
store = Storage(credential_path)
credentials = store.get()
if not credentials or credentials.invalid:
flow = client.flow_from_clientsecrets(self.CLIENT_SECRET_FILE, self.SCOPES)
flow.user_agent = self.APPLICATION_NAME
if flags:
credentials = tools.run_flow(flow, store, flags)
else: # Needed only for compatibility with Python 2.6
credentials = tools.run(flow, store)
print('Storing credentials to ' + credential_path)
return credentials
main.py
from __future__ import print_function
import httplib2
import os, io
from apiclient import discovery
from oauth2client import client
from oauth2client import tools
from oauth2client.file import Storage
from apiclient.http import MediaFileUpload, MediaIoBaseDownload
try:
import argparse
flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
except ImportError:
flags = None
import auth
# If modifying these scopes, delete your previously saved credentials
# at ~/.credentials/drive-python-quickstart.json
SCOPES = 'https://www.googleapis.com/auth/drive'
# CLIENT_SECRET_FILE = 'client_secret.json'
THIS_FOLDER = os.path.dirname(os.path.abspath(__file__))
CLIENT_SECRET_FILE = os.path.join(THIS_FOLDER, 'client_secret.json')
APPLICATION_NAME = 'Drive API Python Quickstart'
authInst = auth.auth(SCOPES,CLIENT_SECRET_FILE,APPLICATION_NAME)
credentials = authInst.getCredentials()
http = credentials.authorize(httplib2.Http())
drive_service = discovery.build('drive', 'v3', http=http)
def uploadFile(filename,filepath,mimetype):
file_metadata = {'name': filename}
media = MediaFileUpload(filepath,
mimetype=mimetype)
file = drive_service.files().create(body=file_metadata,
media_body=media,
fields='id').execute()
print('File ID: %s' % file.get('id'))
API控制台和输出中的凭据和同意设置
“同意”屏幕仅输入“应用程序名称”,通过电子邮件发送的ID,“范围定义”为空。将下载Json文件并将其移至工作目录。
浏览器显示错误消息。
4,5和6-尝试绕过并手动提供访问权限,结果显示为“身份验证流程已完成”,但无法将文件上传到驱动器。
我已经阅读了SO和其他网站上有关此主题的其他文档,但是我无法找到为什么身份验证不起作用的原因,同意屏幕显示了验证状态-未发布,并且没有任何消息要求进行验证。了解这篇文章很长,感谢您的耐心等候。