简体   繁体   中英

Batch requests with Google's Client Library with Javascript

I'm trying to request multiple records using google's client library api. I'm attempting to get a list of students and a separate list of assignments that are affiliated with a single google class. I'm using the google classroom api ( https://developers.google.com/classroom/reference/rest/ ).

Here's what I've got so far:

    let batch = gapi.client.newBatch();

    let courseWorkRequest = function(courseId) {
        return gapi.client.request({
            'path': `/v1/courses/${courseId}/courseWork`,
        });
    };

    let studentRequest = function (courseId) {
        return gapi.client.request({
            'path': `/v1/courses/${courseId}/students`
        });
    };

    listOfGoogleClasses.forEach(function (course) {
        let courseAssignments = courseWorkRequest(course.id);
        batch.add(courseAssignments);
        let courseStudents = studentRequest(course.id);
        batch.add(courseStudents)
    });

    batch.then(function(response){
        console.log(response);
    });

The request works but for the response, I'm just getting a series of objects that look like so:

  body:"Not Found"
  headers:Object
  result:false
  status:404
  statusText: "Not Found"

Deducing from the error itself, it means you're missing some required properties of the request body like Content-Type, Content-Length, etc. The example can be seen in Example batch request

POST https://classroom.googleapis.com/batch HTTP/1.1
Authorization: Bearer your_auth_token
Content-Type: multipart/mixed; boundary=batch_foobarbaz
Content-Length: total_content_length

--batch_foobarbaz
Content-Type: application/http
Content-Transfer-Encoding: binary
MIME-Version: 1.0
Content-ID: <item1:12930812@classroom.example.com>

PATCH /v1/courses/134529639?updateMask=name HTTP/1.1
Content-Type: application/json; charset=UTF-8
Authorization: Bearer your_auth_token

{
  "name": "Course 1"
}
--batch_foobarbaz
Content-Type: application/http
Content-Transfer-Encoding: binary
MIME-Version: 1.0
Content-ID: <item2:12930812@classroom.example.com>

PATCH /v1/courses/134529901?updateMask=section HTTP/1.1
Content-Type: application/json; charset=UTF-8
Authorization: Bearer your_auth_token
{
  "section": "Section 2"
}

The google client library is for all Google APIs. Therefore I think you need to provide the full URL in the path.

Try setting path to: https://classroom.googleapis.com/v1/courses/{courseId}/courseWork instead of just /v1/courses/{courseId}/courseWork

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM