Google 教室 API 范围不正确
Google Classrooms API Incorrect Scopes
Oauth 2.0 API 的新手,在解决 Google 教室 API 的问题时遇到了一些问题。我在 URL https://developers.google.com/classroom/quickstart/dotnet 中使用了 Google 给出的示例代码
并且能够成功连接到 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()
我最终发现我的工具创建了一个名为 "token.json" 的文件夹,我在其中将上面的 credPath 标记为 String = "token.json"。如果我重命名或删除此文件夹,它会要求我重新验证并允许连接。
Oauth 2.0 API 的新手,在解决 Google 教室 API 的问题时遇到了一些问题。我在 URL https://developers.google.com/classroom/quickstart/dotnet 中使用了 Google 给出的示例代码 并且能够成功连接到 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()
我最终发现我的工具创建了一个名为 "token.json" 的文件夹,我在其中将上面的 credPath 标记为 String = "token.json"。如果我重命名或删除此文件夹,它会要求我重新验证并允许连接。