教室API-使用服务帐户访问教室

时间:2019-08-26 17:32:31

标签: google-api asp.net-core-webapi google-oauth2 service-accounts google-classroom

我需要帮助,因为我真的被卡住了!!!!我花了几天和几个小时在这个上,但我不知道。我搜索了所有可能的论坛和其他类似的帖子,但均未成功。

因此,要求:

我们正在尝试在我们的.Net平台上集成Classroom API。棘手的部分是他们想使用服务帐户。

问题:

Google.Apis.Requests.RequestError呼叫者没有权限[403]  呼叫者没有权限]位置[-]原因[禁止]域[全局]

我已经按照文档中的说明进行操作:

https://developers.google.com/identity/protocols/OAuth2ServiceAccount

我了解到我们需要在Google API控制台中设置一个服务帐户,因此我已完成以下操作:

1)我已经创建了一个服务帐户,并在Google API控制台中启用了G Suite域范围委派

2)在管理控制台的“管理API客户端访问”中,我输入了服务帐户的客户端ID并启用了作用域。

3)A已经下载了具有所有服务帐户凭据(私钥,电子邮件)的json文件

和代码...

ServiceAccountCredential credential = new ServiceAccountCredential(
 new ServiceAccountCredential.Initializer(cr.client_email)
           {
               Scopes = new[] { ClassroomService.Scope.ClassroomCourses },

           }.FromPrivateKey(cr.private_key));

        // Create the service.
        service = new ClassroomService(new BaseClientService.Initializer()
        {
            HttpClientInitializer = credential,
            ApplicationName = "Demo-School",
        });

以及对Classroom API的实际调用以创建课程:

var resource = service.Courses.Create(course);
var result = await resource.ExecuteAsync();

因此,尽管如此,每当我尝试创建课程时,都会出现上述错误。

当我尝试使用“参考”页面(https://developers.google.com/classroom/reference/rest/v1/courses/create)创建课程时,它可以正常工作。我可以创建课程,教师,设置权限等。

但是,当我尝试以编程方式执行此操作时。例如,从控制台应用程序中,我无法使其正常工作。

任何人都可以请教吗???????我想念什么??

0 个答案:

没有答案