【问题标题】:Official Google Classroom API example returns error on request C#官方 Google Classroom API 示例在请求 C# 时返回错误
【发布时间】:2021-05-29 20:54:42
【问题描述】:

大家好,我遇到了 Google Classroom API 和 C# 的问题。我希望能够从不和谐机器人创建课程,但由于遇到问题,我决定尝试使用来自 source 的简单示例

连接了一些额外的库,但这只是一个小问题。问题是我从官方 google api 网站粘贴的这段代码返回错误。我在网上搜索了解决方案并找到了一些案例。有人要求在 Google Cloud Console 中打开 API,我在我的项目中打开了 Google Classroom API。但这没有帮助。但我可以从 google 帐户的教室中读取信息,例如课程名称等。

这是对 Google API 的全新(也是第一次)体验,例如我不知道一些基本知识?我该怎么办?

using Google.Apis.Auth.OAuth2;
using Google.Apis.Classroom.v1;
using Google.Apis.Classroom.v1.Data;
using Google.Apis.Services;
using Google.Apis.Util.Store;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ClassroomQuickstart
{
    class Program
    { 
        static string[] Scopes = { ClassroomService.Scope.ClassroomCoursesReadonly };
        static string ApplicationName = "Classroom API .NET Quickstart";

        static void Main(string[] args)
        {
            UserCredential credential;

            using (var stream =
                new FileStream("credentials.json", FileMode.Open, FileAccess.Read))
            {
                // The file token.json stores the user's access and refresh tokens, and is created
                // automatically when the authorization flow completes for the first time.
                string credPath = "token.json";
                credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                    GoogleClientSecrets.Load(stream).Secrets,
                    Scopes,
                    "user",
                    CancellationToken.None,
                    new FileDataStore(credPath, true)).Result;
                Console.WriteLine("Credential file saved to: " + credPath);
            }

            // Create Classroom API service.
            var service = new ClassroomService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName = ApplicationName,
            });
            Console.WriteLine("Service");

            // Define request parameters.
            CoursesResource.ListRequest request = service.Courses.List();
            request.PageSize = 10;

            var course = new Course
            {
                Name = "10th Grade Biology",
                Section = "Period 2",
                DescriptionHeading = "Welcome to 10th Grade Biology",
                Description = "We'll be learning about about the structure of living creatures "
                    + "from a combination of textbooks, guest lectures, and lab work. Expect "
                    + "to be excited!",
                Room = "301",
                OwnerId = "me",
                CourseState = "PROVISIONED"
            };
            Console.WriteLine("Course");

            course = service.Courses.Create(course).Execute();
            Console.WriteLine("Execute");
            Console.WriteLine("Course created: {0} ({1})", course.Name, course.Id);

        }
    }
}

错误

Google.GoogleApiException: "Google.Apis.Requests.RequestError
Request had insufficient authentication scopes. [403]
Errors [
    Message[Insufficient Permission] Location[ - ] Reason[insufficientPermissions] Domain[global]

【问题讨论】:

  • 您要求在您的范围内进行只读访问,然后尝试创建课程..?
  • @stuartd,这是我这辈子遇到的最大的麻烦。 ://
  • @stuartd 所以我将 Scopes 从 ClassroomCoursesReadonly 更改为 ClassroomCourses 但它没有帮助,我有同样的问题

标签: c# .net google-classroom


【解决方案1】:

问题:

ClassroomCoursesReadonly 不能用于创建课程,只能用于检索课程。

解决办法:

所以我找到了解决方案:

  1. 将范围从只读更改为仅课堂课程
  2. 重新创建凭据
  3. 上传项目文件夹中的新文件

参考:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-08-21
    • 2021-09-08
    • 1970-01-01
    • 1970-01-01
    • 2017-03-20
    • 2019-03-13
    • 2020-11-08
    • 2020-02-16
    相关资源
    最近更新 更多