使用 c# 访问课堂作业附件时出现权限错误

问题描述 投票:0回答:1

通过我在下面放置的方法的代码,我尝试获取课堂作业的附加附件列表。我使用班级老师的帐户进行访问,正如您在开头所看到的,所有可能的权限都已添加。我收到以下错误的问题:

“服务教室抛出异常,HttpStatusCode为Forbidden。请求认证范围不足。”

我的方法是:

public class Classroom
{
    static string[] Scopes = { ClassroomService.Scope.ClassroomCourses, ClassroomService.Scope.ClassroomCourseworkmaterials,
    ClassroomService.Scope.ClassroomCourseworkMe, ClassroomService.Scope.ClassroomCourseworkStudents, ClassroomService.Scope.ClassroomCourseworkStudentsReadonly, ClassroomService.Scope.ClassroomGuardianlinksStudents,
    ClassroomService.Scope.ClassroomAnnouncements, ClassroomService.Scope.ClassroomCourseworkMe, ClassroomService.Scope.ClassroomProfileEmails, ClassroomService.Scope.ClassroomProfilePhotos,
    ClassroomService.Scope.ClassroomPushNotifications, ClassroomService.Scope.ClassroomRosters, ClassroomService.Scope.ClassroomStudentSubmissionsMeReadonly,
    ClassroomService.Scope.ClassroomStudentSubmissionsStudentsReadonly, ClassroomService.Scope.ClassroomTopics};
    static string ApplicationName = "ControlGoogleMCV";

    public GDrive_baj GDrive_bajD = new GDrive_baj();
public void Main()
{
    ClientSecrets secrets = new ClientSecrets()
    {
        ClientId = "________________",
        ClientSecret = "__________________________"
    };

    UserCredential credential;

    using (var stream =
      new FileStream(Form1.ObtenerAtributo("rutalocaljson"), FileMode.Open, FileAccess.Read))
    {
  
        string credPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
        credPath = Path.Combine(credPath, ".credentials/", System.Reflection.Assembly.GetExecutingAssembly().GetName().FullName, "paraclassroom");
        credential = Google.Apis.Auth.OAuth2.GoogleWebAuthorizationBroker.AuthorizeAsync(secrets,
            Scopes,
            "_________",
            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,
    });

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

    try
    {
        CoursesResource.ListRequest lr = new CoursesResource.ListRequest(service);

        string cadbusname = Interaction.InputBox("Cadena de búsqueda en el atributo ''name'' del curso. ''Cancelar'' no " +
            "dará resultado; ''Aceptar'' inicia la búsqueda", "Cadena de búsqueda en el nombre del curso", "Transcripción de la música flamenca 2024/2025"
        );
        if (cadbusname.Length > 0) {
            foreach (Course c in lr.Execute().Courses)
            {
                if (c.Name.Contains(cadbusname))
                {
                    string cadbusnametarea = Interaction.InputBox("Cadena de búsqueda en el atributo ''name'' de la tarea. ''Cancelar''" +
       "no dará resultado; ''Aceptar'' inicia la búsqueda", "Cadena de búsqueda en el nombre de la tarea", "Ejercicio 8.");
                    CoursesResource.CourseWorkResource.ListRequest cwrlr = new CoursesResource.CourseWorkResource.ListRequest(service, c.Id);

                    foreach (CourseWork cwr in cwrlr.Execute().CourseWork) 
                    {
                        if (cwr.Title.Contains(cadbusnametarea))
                        {                           
                            string idCur = cwr.Id;
                            var cwrmat = cwr.Materials;
                  
                            foreach (var item in cwrmat)
                            {
                                if (item.DriveFile != null)
                                {
                                    string tx = "El archivo se ha subido desde Google Drive. El thumbonailurl es : " + item.DriveFile.DriveFile.ThumbnailUrl +
                                         ". El enlace alternativo es: " + item.DriveFile.DriveFile.AlternateLink + ". El título es: " + item.DriveFile.DriveFile.Title;
                                }
                                if (item.Link != null)
                                {
                                    string tx = "Se trata de un enlace. La url es " + item.Link.Url + ". El thumbonailurl es : " + item.Link.ThumbnailUrl +
                                        ". El título es: " + item.Link.Title;
                                }
                            }

CoursesResource.CourseWorkResource.AddOnAttachmentsResource.ListRequest ccarlr =                               new CourseWorkResource.AddOnAttachmentsResource.ListRequest(service, c.Id, idCur);
                            foreach (AddOnAttachment item in ccarlr.Execute().AddOnAttachments)
                            {
                                string nn = item.ItemId;  
                            }
                        }
                    }
                }
            }
        };               
    }
    catch (Exception e) 
    {
        string ff = e.Message;
    }
}
}

错误出现在行上:

 foreach (AddOnAttachment item in ccarlr.Execute().AddOnAttachments)

我将不胜感激任何帮助。

目标是能够访问与我自己为学生创建的任务附件相关的数据。

我编辑说,我尝试为公告、CourseWorks 和 CoursesorksMaterials 添加附加附件,但它总是给出相同的错误。尽管我已在云平台应用程序中标记了 Classroom API 的所有可能权限,并且它们也出现在我执行这些操作的类的开头,但它永远不允许我访问或创建附加附件。 但是,我可以访问 Courses 和 CourseWorks 的数据,也可以使用同一帐户(教师)创建它们。

此公告创建对我来说正确无误

      //se crea correctamente el anuncio en el tablón
   Google.Apis.Classroom.v1.Data.Announcement anu = new Announcement();
   anu.Text = "El cuerpo del anuncio para el curso Prueba";
   Google.Apis.Classroom.v1.CoursesResource.AnnouncementsResource.CreateRequest cr = new
       CoursesResource.AnnouncementsResource.CreateRequest(service, anu, c.Id);
   cr.Execute();

但是这个 CourseWork 附加附件创建对我不起作用

////insuficientes permisos
 Google.Apis.Classroom.v1.Data.AddOnAttachment addo = new AddOnAttachment();
 addo.Title = "El título del add-on attachment para la tarea Prueba1";
 Google.Apis.Classroom.v1.CoursesResource.CourseWorkResource.AddOnAttachmentsResource.CreateRequest cwaoa = new
    CourseWorkResource.AddOnAttachmentsResource.CreateRequest(service, addo, c.Id, idCur);
 cwaoa.Execute();

我尝试在创建 CourseWork 后立即创建附加附件,以确保错误不是因为 CourseWork 实例具有基于访问模式的访问限制。但同样的错误发生了。 这是代码:

////The task is created correctly when I try to create an add-on attachment for it
   //the error about missing permissions appears again
   Google.Apis.Classroom.v1.Data.CourseWork cw = new CourseWork();
   cw.Title = "New courseWork for the course";
   cw.WorkType = "Assignment";
   cw.State = "PUBLISHED";
   String idCW = "";
   Google.Apis.Classroom.v1.CoursesResource.CourseWorkResource.CreateRequest cwre = 
       new CourseWorkResource.CreateRequest (service, cw, c.Id);
   cwre.Execute();
   foreach (Google.Apis.Classroom.v1.Data.CourseWork item in cwrlr.Execute().CourseWork)
   {
    if (item.Title == cw.Title)
       {
           idCW = item.Id;
           break;
       }
   }

   Google.Apis.Classroom.v1.Data.AddOnAttachment addoA = new AddOnAttachment();
   addoA.Title = "The title of the add-on attachment for the task created";
   Google.Apis.Classroom.v1.CoursesResource.CourseWorkResource.AddOnAttachmentsResource.CreateRequest cwaoa = new
      CourseWorkResource.AddOnAttachmentsResource.CreateRequest(service, addoA, c.Id, idCW);
   cwaoa.Execute();

线路发生错误:

 cwaoa.Execute();
c# google-classroom scopes
1个回答
0
投票

我认为这个错误是准确的;你没有足够的范围。

您正在对课程作业->插件资源进行

list
调用:

CourseWorkResource.AddOnAttachmentsResource.ListRequest

在该方法的文档中,所需的范围是:

  • https://www.googleapis.com/auth/classroom.addons.student
  • https://www.googleapis.com/auth/classroom.addons.teacher

我在你的

Scopes
中没有看到这些。您可以尝试添加这些内容并看看是否可以解决您的问题吗?

© www.soinside.com 2019 - 2024. All rights reserved.