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

相關問題