我试图弄清楚如何使用谷歌驱动器api和python来更新文件权限以及所有权

问题描述 投票:0回答:1

我从the Google Drive API page for managing shares获取代码并对其进行了一些修改以便能够转移特定文件的所有权,但我一直收到以下错误

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

SCOPES = 'https://www.googleapis.com/auth/drive'
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.pickle'):
    with open('token.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()
    # Save the credentials for the next run
    with open('token.pickle', 'wb') as token:
        pickle.dump(creds, token)

file_id = 'XXXXXX'
drive_service = build('drive', 'v3', credentials=creds)
def callback(request_id, response, exception):
    if exception:
        # Handle error
        print(exception)
    else:
        print("Permission Id: %s" % response.get('id'))

batch = drive_service.new_batch_http_request(callback=callback)
user_permission = {
    'type': 'user',
    'role': 'writer',
    'emailAddress': '[email protected]'
}
batch.add(drive_service.permissions().create(
        fileId=file_id,
        body=user_permission,
        fields='id',
))
domain_permission = {
    'type': 'domain',
    'role': 'reader',
    'domain': 'example.com'
}
batch.add(drive_service.permissions().create(
        fileId=file_id,
        body=domain_permission,
        fields='id',
))
batch.execute()

我得到的错误是

<HttpError 403 when requesting https://www.googleapis.com/drive/v3/files/1w43iXVO04QhDl-eWHi75ImSdxu-_V5xp/permissions?fields=id&alt=json returned "Insufficient Permission: Request had insufficient authentication scopes.">
<HttpError 403 when requesting https://www.googleapis.com/drive/v3/files/1w43iXVO04QhDl-eWHi75ImSdxu-_V5xp/permissions?fields=id&alt=json returned "Insufficient Permission: Request had insufficient authentication scopes.">

但我不确定它是代码本身的问题还是我如何设置API的凭据。有人有使用Google Drive API for Python的经验,可以给我一个关于我做错的指针吗?

python-3.x
1个回答
0
投票

可能很明显,但您的凭证没有授权。

你确定加载了正确的credentials.json和范围吗?您可以使用不同api产品的快速入门凭据获得相同的错误。

尝试加载正确的凭据和范围时,请尝试将其注释掉。

if os.path.exists('token.pickle'):
    with open('token.pickle', 'rb') as token:
        creds = pickle.load(token)
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.