Hugo Paz
Hugo Paz

Reputation: 36

ModifyAttachments call returns 400 error using C# client library

The following code works for most of the students in a classroom when they submit an asignment, but it throws an exception with a 400 error for some of the students:

var classroom = GetClassroomService();

var studentSubmissionsResponse = classroom.Courses.CourseWork.StudentSubmissions.List(courseId, courseWorkId).Execute();
var submission = studentSubmissionsResponse.StudentSubmissions.FirstOrDefault(s => s.AssociatedWithDeveloper ?? false);

var modifyAttachmentRequest = new global::Google.Apis.Classroom.v1.Data.ModifyAttachmentsRequest();

var link = new global::Google.Apis.Classroom.v1.Data.Link() { Url = url };
var attachment = new global::Google.Apis.Classroom.v1.Data.Attachment() { Link = link };

modifyAttachmentRequest.AddAttachments = new System.Collections.Generic.List<global::Google.Apis.Classroom.v1.Data.Attachment>();
modifyAttachmentRequest.AddAttachments.Add(attachment);

submission = classroom.Courses.CourseWork.StudentSubmissions.ModifyAttachments(modifyAttachmentRequest, courseId, courseWorkId, submission.Id).Execute();

/// submission code is after this; the exception is thrown by the call above

The exception is:

The service classroom has thrown an exception: Google.GoogleApiException: Google.Apis.Requests.RequestError
Request contains an invalid argument. [400]
Errors [
    Message[Request contains an invalid argument.] Location[ - ] Reason[badRequest] Domain[global]
]

Unfortunately, the C# client library doesn’t currently support the display of detailed error messages yet.

Any ideas on what could be going on? How to troubleshoot?

Upvotes: 0

Views: 489

Answers (2)

AndyDeveloper
AndyDeveloper

Reputation: 3000

Note that you will get a 400 error and an invalid argument exception if you call modifyAttachments on a submission whose state is TURNED_IN.

I can't tell from your code whether some of your students' submissions have already been turned in. In my case, I was adding attachments after calling turnIn and getting the same error. Inverting the order of calls solved it.

Upvotes: 0

TheRock
TheRock

Reputation: 1541

A 400 means that the request was malformed. In other words, the data stream sent by the client to server did not follow the rules.

So what I'd do to troubleshoot this is to start Fiddler, run the code and isolate the request to the endpoint. Maybe then compare the invalid request to a valid one and determine the difference.

Upvotes: 1

Related Questions