c# - 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()
解决方案
我最终发现我的工具创建了一个名为“token.json”的文件夹,我将上面的 credPath 标记为 String =“token.json”。如果我重命名或删除此文件夹,它会要求我重新验证并允许连接。
推荐阅读
- flutter - 如何在飞镖的待办事项中创建子任务?
- c++ - 数组和向量的内存使用情况(64 位)?
- android - 顶部的Android视图ScrollView LinearLayout
- google-document-viewer - 谷歌文档查看器有时会返回空白屏幕(204 响应)
- reactjs - react-datepicker includeTimes 选项
- java - RabbitMQ 正在运行仍然被拒绝连接连接异常
- r - 从R中的csv文件中提取3维数据
- python-3.x - 通过 API 执行时如何获取 AWS Glue 作业的结果?
- javascript - 如何删除重复事件?
- matlab - 用于累积功率的 Matlab 函数