Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Sign-In: backend verification

I have Google Sign-in working on my app: the relevant code is roughly:

var acc = await signInService.signIn();
var auth = await acc.authentication;
var token = auth.idToken;

This gives me a nice long token, which I then pass to my backend with an HTTP POST (this is working fine), and then try to verify. I have the same google-services.json file in my flutter tree and on the backend server (which is nodejs/restify). The backend code is roughly:

let creds = require('./google-services.json');
let auth = require('google-auth-library').OAuth2Client;
let client = new auth(creds.client[0].oauth_client[0].client_id);
. . .
let ticket = await client.verifyIdToken({
    idToken: token,
    audience: creds.client[0].oauth_client[0].client_id
});
let payload = ticket.getPayload();

This consistently returns my the error "Wrong recipient, payload audience != requiredAudience".

I have also tried registering separately with GCP console and using those keys/client_id instead, but same result. Where can I find the valid client_id that will properly verify this token?

like image 281
Lee Daniel Crocker Avatar asked Jan 11 '19 00:01

Lee Daniel Crocker


People also ask

What is Id_token Google OAuth?

The id_token is used in OpenID Connect protocol, where the user is authenticated as well as authorized. (There's an important distinction between authentication and authorization.) You will get id_token and access_token. The id_token value contains the information about the user's authentication.


1 Answers

Another quick solution might be change the name of your param "audience" to "requiredAudience". It works to me. If you copied the code from google, maybe the google documentation is outdated.

client.verifyIdToken({
      idToken,
      requiredAudience: GOOGLE_CLIENT_ID,  // Specify the CLIENT_ID of the app that accesses the backend
      // Or, if multiple clients access the backend:
      //[CLIENT_ID_1, CLIENT_ID_2, CLIENT_ID_3]
  });
like image 118
David117Master Avatar answered Sep 20 '22 03:09

David117Master