GoogleAPIOAuth2登录新OAuth2客户端出现问题

我正在尝试制作一个与谷歌 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']

然后我使用 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:
        flow = InstalledAppFlow.from_client_secrets_file(CLIENTSECRETPATH, SCOPES)
        cred = flow.run_local_server()

    with open("token.pickle", "wb") as token:
        pickle.dump(cred, token)

try:
    service = build(APISERVICENAME, APIVERSION, credentials=cred)

except Exception as e:
    print(e)

编辑:我尝试更改 google 控制台上的一些设置,并随机决定单击发布,因为该项目仍处于测试状态,执行此操作后,我可以无错误地登录。但这仍然不能解释为什么它在测试状态下不起作用,我将我的学校电子邮件地址添加到测试用户列表中,并确保我为测试做了正确的一切。

出了点问题截图

回答

我无法回复上述问题(需要更多声誉),但可以确认我看到了相同的行为。更奇怪的是,该问题仅在用户尝试使用已登录的帐户执行 OAuth 集成时才会出现。再试一次。” 甚至在看到所需的范围列表之前就出错。但是,如果用户未登录其帐户,而是作为 OAuth 集成的一部分登录,则不会出现错误并且可以成功完成集成。此问题不会影响未登录的用户这一事实表明设置(回调 API、credentials.json 等)都是正确的。我相信这个问题是在上个月左右引入的。


以上是GoogleAPIOAuth2登录新OAuth2客户端出现问题的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>