2017-04-18 114 views
1

我试图使用谷歌的客户端库API请求多个记录。 我正试图获得学生名单以及与单个Google课程相关的单独作业列表。我正在使用google教室API(https://developers.google.com/classroom/reference/rest/)。使用Javascript的Google客户端库的批量请求

这里是我到目前为止有:

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); 
    }); 

请求的作品,但对于响应,我只是得到了一系列的对象,看起来像这样:

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

回答

0

推导从错误本身来看,这意味着您缺少Content-Type,Content-Length等请求主体的一些必需属性。此示例可参见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:[email protected]> 

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:[email protected]> 

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

谷歌客户端库适用于所有Google API。因此我认为你需要在路径中提供完整的网址。

尝试设置路径:https://classroom.googleapis.com/v1/courses/{courseId}/courseWork ,而不是仅仅/v1/courses/{courseId}/courseWork

相关问题