Google Classrooms API范围不正确



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

我现在正试图使用谷歌的建议要求访问课堂公告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()

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

最新更新