我试图弄清楚如何通过API编辑谷歌幻灯片,但我得到授权范围错误,
googleapiclient.errors.HttpError: <HttpError 403 when requesting https://slides.googleapis.com/v1/presentations/1Sqc7C8q2bF1Ay65sO-nZgEJNGuzGBgXIpxqv7hc4v6g:batchUpdate?alt=json returned "Request had insufficient authentication scopes.". Details: "[{'@type': 'type.googleapis.com/google.rpc.ErrorInfo', 'reason': 'ACCESS_TOKEN_SCOPE_INSUFFICIENT', 'domain': 'googleapis.com', 'metadata': {'service': 'slides.googleapis.com', 'method': 'google.apps.slides.v1.PresentationsService.BatchUpdatePresentation'}}]">
('reason':'ACCESS_TOKEN_SCOPE_INSUFFICIENT')
我的代码用来做这件事的是:
from __future__ import print_function
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
import os.path
from googleapiclient.errors import HttpError
import json
# If modifying these scopes, delete the file token.json.
SCOPES = ['https://www.googleapis.com/auth/presentations.readonly']
# The ID of a sample presentation.
PRESENTATION_ID = 'LEWL'
creds = None
# The file token.json 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.json'):
creds = Credentials.from_authorized_user_file('token.json', SCOPES)
# 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.json', 'w') as token:
token.write(creds.to_json())
service = build('slides', 'v1', credentials=creds)
#
# Add a slide at index 1 using the predefined
# 'TITLE_AND_TWO_COLUMNS' layout and the ID page_id.
requests = [
{
'createSlide': {
'objectId': 'titlepage',
'insertionIndex': '1',
'slideLayoutReference': {
'predefinedLayout': 'TITLE_AND_TWO_COLUMNS'
}
}
}
]
# If you wish to populate the slide with elements,
# add element create requests here, using the page_id.
# Execute the request.
body = {
'requests': requests
}
response = service.presentations() \
.batchUpdate(presentationId=PRESENTATION_ID, body=body).execute()
create_slide_response = response.get('replies')[0].get('createSlide')
print(f"Created slide with ID:"
f"{(create_slide_response.get('objectId'))}")
此外,我不知道如何修改范围,所以如果这是问题的意见将不胜感激
还有一件事要提的是,我可以阅读数据从谷歌幻灯片没有问题,但编辑不工作
1条答案
按热度按时间u7up0aaq1#
错误消息
ACCESS_TOKEN_SCOPE_INSUFFICIENT
表示:请求被拒绝,因为提供的访问令牌至少没有API所需的一个可接受范围
代码中定义的作用域为:
来自OAuth 2.0 Scopes for Google Slides API
要编辑幻灯片,请求应具有以下范围:
要修改作用域,请按如下方式更改SCOPE变量: