Reputation: 51
With the code of the method that I put below I try to obtain the list of add-on attachments of a Classroom assignment. I access with the account of the teacher of the class and as you can see at the beginning all the possible permissions are added. The problem for I get the following error:
"The service classroom has thrown an exception. HttpStatusCode is Forbidden. Request had insufficient authentication scopes."
My method is:
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;
}
}
}
The error appears on the line:
foreach (AddOnAttachment item in ccarlr.Execute().AddOnAttachments)
I would appreciate any help.
The goal is to be able to access the data relating to the attachments of the tasks that I myself have created for the students.
I edit to say that I have tried adding add-on attachments for Announcements, CourseWorks and CoursesorksMaterials and it always gives the same error. Even though I have marked all the possible permissions for the Classroom API in my Cloud Platform application and they also appear at the beginning of the class in which I execute these operations, it never allows me to access or create the add-on attachments. However, I can access the data for Courses and CourseWorks, and I can also create them with the same account (teacher).
This Announcement creation works for me correctly
//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();
But this CourseWork add-on attachment creation doesn't work for me
////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();
I have tried creating the add-on attachment immediately after creating the CourseWork to make sure that the error is not because the CourseWork instance has access restrictions based on the access mode. But the same error occurs. Here is the code:
////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();
The error occurs on the line:
cwaoa.Execute();
Upvotes: 0
Views: 179
Reputation: 3001
I think the error is accurate; you don't have sufficient scopes.
You're making a list
call on the coursework->addons resource:
CourseWorkResource.AddOnAttachmentsResource.ListRequest
In the documentation for that method, the required scopes are:
https://www.googleapis.com/auth/classroom.addons.student
https://www.googleapis.com/auth/classroom.addons.teacher
I don't see those in your Scopes
. Can you try adding those and seeing if that resolves your issue?
Upvotes: 0