15

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 = 'redacted@redacted.iam.gserviceaccount.com'
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

4 Answers4

12

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

possen
  • 8,596
  • 2
  • 39
  • 48
Rahav Lussato
  • 121
  • 1
  • 5
1

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.

Vasucd
  • 357
  • 2
  • 10
0

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

0

This is not answer to fix the issue, just my exp, actually sounds like two previous answer.

I have a same error log when I build Flutter app then I change base api url and some other env variable to run code in staging env but acttually still run in main_development.dart. I realized that my app still uses Firebase app (which defined by Google service info) of development env not staging env.

So I just run right way again (defined in launch.json) - not lazy (just substitute base api url) and it works.

Quyen Anh Nguyen
  • 1,204
  • 13
  • 21