使用Google Slides API的身份验证范围不足错误

时间:2020-07-09 06:00:20

标签: python google-slides-api

我正试图了解如何使用Google Slides API,

我做了here所述的一切。

在“启用Google Slides API”中,我尝试选择“桌面应用”或“ Web服务器”,但是每次尝试运行quickstart.py文件时,这两种方式都会出现相同的错误:

googleapiclient.errors.HttpError:https://slides.googleapis.com/v1/presentations/1EAYk18WDjIG-zp_0vLm3CsfQh_i8eXc67Jo2O9C6Vuc?alt=json返回时,“请求的身份验证范围不足。“>

这就是我的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

# If modifying these scopes, delete the file token.pickle.
SCOPES = ['https://www.googleapis.com/auth/presentations.readonly']

# The ID of a sample presentation.
PRESENTATION_ID = '1EAYk18WDjIG-zp_0vLm3CsfQh_i8eXc67Jo2O9C6Vuc'

def main():
    """Shows basic usage of the Slides API.
    Prints the number of slides and elments in a sample presentation.
    """
    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(port=0)
        # Save the credentials for the next run
        with open('token.pickle', 'wb') as token:
            pickle.dump(creds, token)

    service = build('slides', 'v1', credentials=creds)

    # Call the Slides API
    presentation = service.presentations().get(
        presentationId=PRESENTATION_ID).execute()
    slides = presentation.get('slides')

    print('The presentation contains {} slides:'.format(len(slides)))
    for i, slide in enumerate(slides):
        print('- Slide #{} contains {} elements.'.format(
            i + 1, len(slide.get('pageElements'))))


if __name__ == '__main__':
    main()

已更新:

整个错误消息如下:

Traceback (most recent call last):
  File "/Users/A/PycharmProjects/PyProjects/quickstart.py", line 54, in <module>
    main()
  File "/Users/A/PycharmProjects/PyProjects/quickstart.py", line 44, in main
    presentationId=PRESENTATION_ID).execute()
  File "/Users/A/PycharmProjects/PyProjects/venv/lib/python3.7/site-packages/googleapiclient/_helpers.py", line 134, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "/Users/A/PycharmProjects/PyProjects/venv/lib/python3.7/site-packages/googleapiclient/http.py", line 907, in execute
    raise HttpError(resp, content, uri=self.uri)
googleapiclient.errors.HttpError: <HttpError 403 when requesting https://slides.googleapis.com/v1/presentations/1EAYk18WDjIG-zp_0vLm3CsfQh_i8eXc67Jo2O9C6Vuc?alt=json returned "Request had insufficient authentication scopes.">

1 个答案:

答案 0 :(得分:0)

您好,我偶然发现了一个易于解决的问题,您只需手动批准即可。

https://developers.google.com/people/quickstart/python#step_1_turn_on_the

因此,按“启用People API”按钮,它将打开一个新窗口,您必须在第一次手动登录才能获得批准,希望对您有所帮助。