首页 > 解决方案 > Google Classrooms API 范围不正确

问题描述

Oauth 2.0 API 的新手,在找出 Google Classrooms API 的问题时遇到了一些麻烦。我使用了谷歌在这个 URL https://developers.google.com/classroom/quickstart/dotnet上提供的示例代码, 并且能够成功连接到 API 并下拉一些课程信息。

我现在正在尝试使用来自 google https://developers.google.com/classroom/reference/rest/v1/courses.announcements/list的建议要求来访问课堂公告。我将代码修改如下,但收到错误消息,指出 Google.Apis.Requests.RequestError 请求的身份验证范围不足。[403]。在下面的代码中,您可以看到我已经实现了正确的范围

    Dim credential As UserCredential
    Dim Scopes2 As String() = {ClassroomService.Scope.ClassroomAnnouncements, ClassroomService.Scope.ClassroomAnnouncementsReadonly}
    Using stream = New FileStream("credentials.json", FileMode.Open, FileAccess.Read)
        Dim credPath As String = "token.json"
        credential = GoogleWebAuthorizationBroker.AuthorizeAsync(GoogleClientSecrets.Load(stream).Secrets, Scopes2, "user", CancellationToken.None, New FileDataStore(credPath, True)).Result
        Console.WriteLine("Credential file saved to: " & credPath)
    End Using

    Dim service = New ClassroomService(New BaseClientService.Initializer() With {
        .HttpClientInitializer = credential,
        .ApplicationName = ApplicationName
    })

    Dim request As CoursesResource.AnnouncementsResource.ListRequest = service.Courses.Announcements.List("70506149429")
    Dim response As ListAnnouncementsResponse = request.Execute()


    Console.WriteLine("CourseAnnouncements:")

    If response.Announcements IsNot Nothing AndAlso response.Announcements.Count > 0 Then

        For Each announcement In response.Announcements
            Console.WriteLine("{0} ({1})", announcement.Text, announcement.Id)
        Next
    Else
        Console.WriteLine("No announcement found.")
    End If

    Console.Read()

标签: c#google-classroom

解决方案


我最终发现我的工具创建了一个名为“token.json”的文件夹,我将上面的 credPath 标记为 String =“token.json”。如果我重命名或删除此文件夹,它会要求我重新验证并允许连接。


推荐阅读