Estoy tratando de implementar el procesamiento por lotes para mis llamadas GET en javascript, de modo que pueda ejecutarlo. Me encuentro con un error 401 cuando uso el lote (código que se muestra a continuación junto con el error), pero cuando ejecuté llamadas GET individuales, no hubo ningún error y ya me conecté a través de OAUTH2. ¿Hay algún problema con la forma en que estoy construyendo el cuerpo para mi lote? ¡Cualquier ayuda sería apreciada!
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" } }