[英]Batch requests with Google's Client Library with Javascript
我正在嘗試使用谷歌的客戶端庫api請求多個記錄。 我正在嘗試獲取學生列表以及與單個Google課程相關聯的單獨作業列表。 我正在使用谷歌教室api( https://developers.google.com/classroom/reference/rest/ )。
這是我到目前為止所得到的:
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"
從錯誤本身中推斷,這意味着您缺少請求體的一些必需屬性,如Content-Type,Content-Length等。示例可以在示例批處理請求中看到
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"
}
Google客戶端庫適用於所有Google API。 因此,我認為您需要在路徑中提供完整的URL。
嘗試設置路徑: https://classroom.googleapis.com/v1/courses/{courseId}/courseWork
: /v1/courses/{courseId}/courseWork
而不僅僅是/v1/courses/{courseId}/courseWork
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.