我正在尝试制作一个与谷歌 API 交互的不和谐机器人,特别是谷歌课堂 API,因此我从谷歌控制台创建了一个新项目,并为 Web 应用程序创建了一个新的 OAuth 客户端。我也启用了 Classroom API 并选择了我想使用的所有范围:
['https://www.googleapis.com/auth/classroom.course-work.readonly',
'https://www.googleapis.com/auth/classroom.student-submissions.students.readonly',
'https://www.googleapis.com/auth/classroom.courses.readonly']
Run Code Online (Sandbox Code Playgroud)
然后我使用 Google 的示例设置了我的 python 程序(起初我使用文档编写了自己的程序,但得到了相同的结果)。当我运行示例代码时一切正常,它打开浏览器并要求我选择我的帐户,我选择我的学校帐户,当它加载时,我希望授权屏幕出现,询问我是否允许请求的数据说出了点问题,根本没有错误消息。我已经credentials.json
从谷歌仪表板下载了正确的文件夹并在我的程序中使用了它。
我还将提供我编写的简化代码,也许那里有问题。
import pickle
import os
from google_auth_oauthlib.flow import Flow, InstalledAppFlow
from googleapiclient.discovery import build
from google.auth.transport.requests import Request
CLIENTSECRETPATH = "credentials.json"
APISERVICENAME = "classroom"
APIVERSION = "v1"
SCOPES = ['https://www.googleapis.com/auth/classroom.course-work.readonly', 'https://www.googleapis.com/auth/classroom.student-submissions.students.readonly', 'https://www.googleapis.com/auth/classroom.courses.readonly']
cred = None
if os.path.exists("toke.pickle"):
with open("tiken.pickle", "rb") as token:
cred = pickle.load(token)
if not cred or not cred.valid:
if cred and cred.expired and cred.refresh_token:
cred.refresh(Request())
else: …
Run Code Online (Sandbox Code Playgroud)