Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

338
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda