gpt4 book ai didi

javascript - 使用 Javascript 通过 Google 客户端库进行批量请求

转载 作者:行者123 更新时间:2023-11-28 04:44:44 25 4
gpt4 key购买 nike

我正在尝试使用 Google 的客户端库 API 请求多个记录。我正在尝试获取与单个谷歌类(class)相关的学生列表和单独的作业列表。我正在使用 Google Classroom 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 等。示例可以在 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"
}

关于javascript - 使用 Javascript 通过 Google 客户端库进行批量请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43481382/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com