Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

343
Vistas
401 Error when batching on Gmail API, but not during individual GET calls (Javascript)

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"
  }
}
about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda