简体   繁体   English

使用Google的客户端库使用Javascript批量请求

[英]Batch requests with Google's Client Library with Javascript

I'm trying to request multiple records using google's client library api. 我正在尝试使用谷歌的客户端库api请求多个记录。 I'm attempting to get a list of students and a separate list of assignments that are affiliated with a single google class. 我正在尝试获取学生列表以及与单个Google课程相关联的单独作业列表。 I'm using the google classroom api ( https://developers.google.com/classroom/reference/rest/ ). 我正在使用谷歌教室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 从错误本身中推断,这意味着您缺少请求体的一些必需属性,如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"
}

The google client library is for all Google APIs. Google客户端库适用于所有Google API。 Therefore I think you need to provide the full URL in the path. 因此,我认为您需要在路径中提供完整的URL。

Try setting path to: https://classroom.googleapis.com/v1/courses/{courseId}/courseWork instead of just /v1/courses/{courseId}/courseWork 尝试设置路径: https://classroom.googleapis.com/v1/courses/{courseId}/courseWork/v1/courses/{courseId}/courseWork而不仅仅是/v1/courses/{courseId}/courseWork

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Google Places Api 批量请求 Javascript - Google Places Api Batch Requests Javascript 批量请求Google地方信息(Javascript v3) - Batch requests for Google Places (Javascript v3) 在Meteor中加载Google JavaScript客户端库 - Load Google JavaScript Client library in Meteor JavaScript中的Google API客户端库无法在本地运行 - Google API client library in javascript not working locally 使用javascript将请求批量上传到Google云端存储 - Batch upload requests to Google Cloud Storage using javascript 是否有一个javascript客户端库,用于排队具有存储支持的ajax请求 - Is there a javascript client-side library for queuing ajax requests with storage support 使用JavaScript的Google API客户端库访问Google Sites API - Using Google APIs Client Library for JavaScript to access Google Sites API 适用于JavaScript的Google API客户端库 - 404方法调用 - Google APIs Client Library for JavaScript - 404 on method call 将Google API Javascript客户端库加载到Chrome扩展程序中 - Loading Google API Javascript Client Library into Chrome Extension 验证服务帐户以使用JavaScript客户端库调用Google API - authenticating a service account to call a Google API with JavaScript client library
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM