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

210
Vistas
Bad Request when fetching id token from Google

This may be a newbiew question (I haven't used the fetch api before), but I can't figure out what wrong with my request.

fetch('https://securetoken.googleapis.com/v1/token?key=' + API_KEY, {
  method: 'POST',
  headers: {'Content-Type': 'application/x-www-form-urlencoded'},
  body: 'grant_type=refresh_token&refresh_token=' + refreshToken
})
.then(response => console.log(response))
.catch(error => console.error(error))

I'm trying to exchange a refresh token for an id token following the guidelines here, but for some reason I'm getting a Bad Request response...

Response { type: "cors", url: "https://securetoken.googleapis.com/v1/token?key=[API_KEY]", redirected: false, status: 400, ok: false, statusText: "Bad Request", headers: Headers, body: ReadableStream, bodyUsed: false }

My key is correct, and the refreshToken is also straight from a response from a service on Firebase SDK.

Where exactly is my mistake?

UPDATE

  1. Showing the context where fetch is executed in a Next.js app:
  2. I'm running this code in dev (localhost) using Firebase Emulators.
  3. I managed to find additional error logs that state { code: 400, message: "INVALID_REFRESH_TOKEN", status: "INVALID_ARGUMENT" }.

So, this indeed seems to be an issue with the refresh_token. Can it be because it has been emitted by Firebase Emulators?

useEffect(() => {

  return firebase.auth().onIdTokenChanged(async user => {

    if (user) {

      fetch('https://securetoken.googleapis.com/v1/token?key=' + process.env.NEXT_PUBLIC_FIREBASE_API_KEY, {
        method: 'POST',
        headers: {'Content-Type': 'application/x-www-form-urlencoded'},
        body: 'grant_type=refresh_token&refresh_token=' + user.refreshToken
      })
      .then(response => console.log(response))
      .catch(error => console.error(error))
    }
  })
}, [])
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

In the end the cause for the issue was the fact that the refreshToken issued when using Firebase Emulators is not valid when exchanging for an idToken.

Quite an edge case, but perhaps someone may find this helpful.

about 4 years ago · Juan Pablo Isaza Denunciar
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