【问题标题】:Request had insufficient authentication scopes for classroom announcements请求的课堂公告的身份验证范围不足
【发布时间】:2020-11-24 09:13:56
【问题描述】:

我正在使用 python api 从教室中获取通知。

我的代码:

from __future__ import print_function
import pickle
import os.path
from os import getcwd
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request

class ClassRoom:
    # If modifying these scopes, delete the file token.pickle.
    def announcements_list(self):
        self.SCOPES = ['https://www.googleapis.com/auth/classroom.courses.readonly', 
                       'https://www.googleapis.com/auth/classroom.announcements.readonly']
        """Shows basic usage of the Classroom API.
        Prints the names of the first 10 courses 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(
                    'credentials.json', self.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('classroom', 'v1', credentials=creds)

        # Call the Classroom API
        results = service.courses().list(pageSize=10).execute()
        courses = results.get('courses', [])
        announcements = []
        for course in course:
            announcements += service.courses().announcements().list(courseId=course["id"], 
                             pageSize=1).execute().get("annoucements")
        return announcements

cs = ClassRoom()
print(cs.announcements_list())

但是脚本正在生成类似 HttpError 403 的错误 “请求的身份验证范围不足。”。详细信息:“请求的身份验证范围不足。”

如何解决这个问题?

【问题讨论】:

  • 更改范围后,您是否删除了令牌文件以触发新的授权流程?
  • 是,但没有帮助
  • 您是教师、学生还是管理员?您是否有权访问指定的课程及其公告?您是否使用 Try this API 功能进行了测试?

标签: python api google-classroom


【解决方案1】:

我有同样的问题,从你的文件夹中删除 token.pickle 并再次执行。它应该工作

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-09
    • 2021-08-07
    • 2021-01-04
    • 1970-01-01
    • 2021-10-20
    • 2021-10-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多