首页 > 解决方案 > 上传文件[PYTHON]时如何解决“403身份验证范围不足”

问题描述

.json每当我关闭程序时,我都会尝试将一些文件保存到我的谷歌驱动器中。但是,我收到此错误: HttpError: <HttpError 403 when requesting https://www.googleapis.com/upload/drive/v3/files?fields=id&alt=json&uploadType=multipart returned "Insufficient Permission: Request had insufficient authentication scopes.">

我大约 30 分钟前开始使用这个模块,并且正在使用我在网站上找到的内容,所以它可能非常明显,但这是我的代码:

SCOPES = 'https://www.googleapis.com/auth/drive'

store = file.Storage('credentials.json')

creds = None
if os.path.exists('token.pickle'):
        with open('token.pickle', 'rb') as token:
            creds = pickle.load(token)

drive_service = build('drive', 'v3', credentials=creds)

# updating function
async def googledriveupdate(file):

    file_metadata = {'name': file}
    media = MediaFileUpload('{}'.format(file),
                            mimetype='application/json')
    file = drive_service.files().create(body=file_metadata,
                                        media_body=media,
                                        fields='id').execute()
    print('File ID: ' + file.get('id'))

这个想法是每当我关闭程序时,都会调用这个函数并且我的所有文件都会被更新。但是,我收到 403 错误并且似乎无法修复它。知道这里发生了什么吗?

标签: pythongoogle-apigoogle-drive-api

解决方案


问题不是我的范围,而是我的泡菜文件。

如果您也遇到此问题,请尝试删除您的 pickle 文件,然后再次运行该程序- 这对我有用


推荐阅读