从Python读取Google表格中的注释所需的身份验证范围是什么

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

我无法弄清楚从文件中获取注释所需的身份验证范围。我运行了这段代码:

SCOPES = ['https://spreadsheets.google.com/feeds',
         'https://www.googleapis.com/auth/drive']
"""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.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(
            r'C:\Users\morton.hsiao\OneDrive - 247 Customer Pvt. Ltd\workspace\247\DriveApiCredentials.json', SCOPES)
        creds = flow.run_local_server(port=0)
    # Save the credentials for the next run
    with open('token.pickle', 'wb') as token:
        pickle.dump(creds, token)

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

# Call the Drive v3 API
results = service.files().list(
    pageSize=1, fields="nextPageToken, files(id, name)").execute()
items = results.get('files', [])

if not items:
    print('No files found.')
else:
    print('Files:')
    for item in items:
        print(u'{0} ({1})'.format(item['name'], item['id']))

这很好。然后我运行了这个:

x = service.comments().list(fileId='FILEID).execute()
items = x.get('files', [])
print(items)

我得到:HttpError:https://www.googleapis.com/drive/v3/files/FILEID/comments?alt=json返回“权限不足:请求的身份验证范围不足。“>

我如何获得足以满足诸如此类的作用域的列表?

google-drive-api
1个回答
0
投票
我知道了。这是Drive API v3的一些提示。

[[本文档] [1]对于非程序员来说确实很令人沮丧,但我现在知道它的错,因此我不是白痴。不读。这是两个主要错误。1. fileId不是一个位置参数,如本文档所示,您可能会相信。您必须像VBA一样指定它,并在其前面加上单词。他们称其为命名参数2.您需要一个fields参数。您可以将其指定为fields =“ *”

这是更好的[link] [2]。这不是一个容易找到的链接。如果您通过前门,则此[link] [3]会导致404错误。他们告诉你的范围是错误的。对我有用的范围是:['googleapis.com/auth/spreadsheets',“ googleapis.com/auth/drive.file”,“ googleapis.com/auth/drive”]。

所以这是我的完整代码:

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/spreadsheets', "https://www.googleapis.com/auth/drive.file", "https://www.googleapis.com/auth/drive"] """Shows basic usage of the Drive v3 API. Prints the names and ids of the first 1 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.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( 'DriveApiCredentials.json', SCOPES) creds = flow.run_local_server(port=0) # Save the credentials for the next run with open('token.pickle', 'wb') as token: pickle.dump(creds, token) service = build('drive', 'v3', credentials=creds) # Call the Drive v3 API results = service.files().list( pageSize=1, fields="nextPageToken, files(id, name)").execute() items = results.get('files', []) if not items: print('No files found.') else: print('Files:') for item in items: print(u'{0} ({1})'.format(item['name'], item['id'])) x = service.comments().list(fileId=secret code from above, fields='*').execute() items = x.get('items', []) print(items) [1]: https://developers.google.com/resources/api-libraries/documentation/drive/v3/python/latest/drive_v3.comments.html [2]: https://developers.google.com/drive/api/v3/reference/comments/list?apix_params=%7B%22fileId%22%3A%2212TBoX2UI_Yu2MA2ZN3p9f-cZsySE4et1slwpgjZbSzw%22%2C%22fields%22%3A%22*%22%7D [3]: https://developers.google.com/drive/api/v3/manage-comments

© www.soinside.com 2019 - 2024. All rights reserved.