13
votes

I'm trying to generate JWT token for Firebase using Ruby on the server. Before 3.0 we used token generator but it stopped working after the upgrade. The token I get with code below gives an error:

The custom token corresponds to a different audience.

and I can't find anywhere what it means.

private_key = OpenSSL::PKey::RSA.new <<-PEM
-----BEGIN PRIVATE KEY-----
..redacted..
-----END PRIVATE KEY-----
PEM

service_account_email = '[email protected]'
now_seconds = Time.now.to_i

payload = {
  iss: service_account_email,
  sub: service_account_email,
  aud: 'https://identitytoolkit.googleapis.com/google.identity.identitytoolkit.v1.IdentityToolkit',
  iat: now_seconds,
  exp: now_seconds + (60 * 60),
  uid: self.id.to_s,
  debug: true,
  claims: {
    userId: self.id,
    slug: self.slug,
    username: self.username,
    avatar: self.profile.avatar.url,
    group: self.group,
    debug: true
  }
}

JWT.encode payload, private_key, 'RS256'

Thanks

3

3 Answers

11
votes

I got this error too, I got that because I used a service account that was not related to the firebase project. After creating new service account with new key under the firebase project its started to work.

For creating service account you can follow the instructions here : https://firebase.google.com/docs/server/setup

0
votes

After spending day on this .I got to know , my GoogleService-Info was wrong. I've tried almost 20+ solution for this .

Resolved it using replacing new GoogleService-Info from the current project in Firebase Console.

0
votes

I get this error too, I solved a problem with change configuration at values FIREBASE_PROJECT_ID and FIREBASE_API_KEY