Backend Verification for Google Sign-In

I have successfully implemented Google Sign-In in my application. Here is a simplified version of my code:

let account = await authenticationService.logIn();
let verification = await account.auth;
let userToken = verification.tokenId;

This token is sent to my backend via an HTTP POST request, which is functioning correctly. On the backend using Node.js and Restify, my implementation looks like this:

const credentials = require('./google-config.json');
const { OAuth2Client } = require('google-auth-library');
const oAuthClient = new OAuth2Client(credentials.apps[0].client_id);
//...
const userTicket = await oAuthClient.verifyIdToken({
    idToken: userToken,
    audience: credentials.apps[0].client_id
});
const userInfo = userTicket.getPayload();

However, I’m encountering an ongoing problem with the message “Incorrect recipient, payload audience does not match the expected audience.” I’ve also attempted to register separately via GCP console and used those keys or client ID, but the issue persists. Where can I locate the correct client ID that would allow for successful token verification?

hey Finn! it’s great that you’ve already got Google Sign-In mostly working! have you checked if the client_id in your google-config.json matches the one in your registered OAuth client IDs in the Google Developer Console? sometimes, environments can get mixed up, like when you dev locally versus deploy to production. do your frontend and backend both use the same client id?

It sounds like the issue might be related to differences in the client ID used in your Google API Console and the one declared in your application code. Ensure that the client ID used in your Node.js backend is exactly the same as the one set in the frontend during the user sign-in process. Additionally, check if your OAuth client is configured for the correct platform, web or mobile, which you are currently using. Sometimes, specific settings for environments, like JavaScript origin validation, could also cause this mismatch.

hey, i had a similar issue once! check your google cloud project settings to make sure the client id is for the correct environment (like dev or prod). also, verifying if your Google Sign-In API is enabled for the current project might help!