I am trying to implement batching for my GET calls in javascript, so that I can it up when running. I am running into a 401 error when using the batch (code shown below along with the error), but when I ran individual GET calls, there was no error, and I already connected through OAUTH2 Is there an issue with how I am constructing the body for my batch? Any help would be appreciated!
var part1 = '--batch_boundary\nContent-Type: application/http\n\Content-ID: '
var part2 = '\nGET https://gmail.googleapis.com/gmail/v1/users/me/messages/'
var body = ''
var numChunks = Math.ceil(messages.length / 100);
for (var i = 0; i < numChunks; i++) {
for (var j = 1; j <= 100; j++) {
if (100i + (j-1) < messages.length - 1) {
body = body + part1 + j + '\n' + part2 + messages[100i + (j-1)].id + API_KEY + '\n\n';
}
}
body = body + '--batch_boundary--'
console.log(body);
BATCHED = {
headers: {
"Content-Type": "multipart/mixed; boundary=batch_boundary",
Authorization: "Bearer" + token,
},
method: "POST",
body: body,
};
const msgData = await fetch("https://www.googleapis.com/batch/gmail/v1", BATCHED);
console.log(await msgData.text())
body = '';
}
ERROR:
--batch_IS59KYdZ1ozjuM1r9SLqUDDV-Ejv2wMM
Content-Type: application/http
Content-ID: response-1
HTTP/1.1 401 Unauthorized
WWW-Authenticate: Bearer realm="https://accounts.google.com/"
Vary: Origin
Vary: X-Origin
Vary: Referer
Content-Type: application/json; charset=UTF-8
{
"error": {
"code": 401,
"message": "Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.",
"errors": [
{
"message": "Invalid Credentials",
"domain": "global",
"reason": "authError",
"location": "Authorization",
"locationType": "header"
}
],
"status": "UNAUTHENTICATED"
}
}