简体   繁体   中英

Firebase FCM sendAll() private key error: error:0909006C:PEM routines:get_name:no start line

This error message based on its frequent appearance in SO seems to be a general response to a number of errors. I am trying to do a FCM sendAll(...) from a Nestjs microservice backend. I initalized the firebase in main.ts like so:

.......
......
 const app = await NestFactory.createMicroservice<MicroserviceOptions>(
    AppModule,
    {
     transport: Transport.TCP,
     options,
    }
  );
  const configService: ConfigService = app.get(ConfigService);
 
  const adminConfig: ServiceAccount = {
    "projectId": configService.get<string>('FIREBASE_PROJECT_ID'),
    "privateKey": configService.get<string>('FIREBASE_PRIVATE_KEY')
                               .replace(/\\n/g, '\n'),
    "clientEmail": configService.get<string>('FIREBASE_CLIENT_EMAIL'),
   };
    
  firebase.initializeApp({
      credential: firebase.credential.cert(adminConfig),
     });
  
  await app.listen();  
 
}
bootstrap();

My.ENV is like so:

 FIREBASE_PRIVATE_KEY    = "-----BEGIN PRIVATE KEY-----\nMI...\n-----END PRIVATE KEY-----\n",
  FIREBASE_CLIENT_EMAIL   = 'abc@my.com',
  FIREBASE_PROJECT_ID     = 'xxx-genysis'

apparently firebase was initialized and the credentials were ok. I do get an error if I substitute invalid credentials.

However, when I tried to execute the sendAll(...) in my notification.service.ts like so:

  import * as firebase from 'firebase-admin';
......
.....
async  sendNotifications(data) {
const messages = await this.getMessages(data.tokens,data.message,data.title);

    const promises = [];
    for (let i = 0; i + 10 <= messages.length; i += 10) {
        const batch = messages.slice(i, i + 10);
        promises.push(firebase.messaging().sendAll(batch));
    }
    await Promise.all(promises);
    
}

I am getting the following error:

error:0909006C:PEM routines:get_name:no start line
Error: error:0909006C:PEM routines:get_name:no start line
    at Sign.sign (node:internal/crypto/sig:131:29)
    at Object.sign (/Applications/GenysisBuild/GenysisNest/genysis-notifications/node_modules/jsonwebtoken/node_modules/jwa/index.js:152:45)
    at Object.jwsSign [as sign] (/Applications/GenysisBuild/GenysisNest/genysis-notifications/node_modules/jsonwebtoken/node_modules/jws/lib/sign-stream.js:32:24)
    at Object.module.exports [as sign] (/Applications/GenysisBuild/GenysisNest/genysis-notifications/node_modules/jsonwebtoken/sign.js:204:16)
    at ServiceAccountCredential.createAuthJwt_ (/Applications/GenysisBuild/GenysisNest/genysis-notifications/node_modules/firebase-admin/lib/app/credential-internal.js:105:20)
    at ServiceAccountCredential.getAccessToken (/Applications/GenysisBuild/GenysisNest/genysis-notifications/node_modules/firebase-admin/lib/app/credential-internal.js:77:28)
    at FirebaseAppInternals.refreshToken (/Applications/GenysisBuild/GenysisNest/genysis-notifications/node_modules/firebase-admin/lib/app/firebase-app.js:45:49)
    at FirebaseAppInternals.getToken (/Applications/GenysisBuild/GenysisNest/genysis-notifications/node_modules/firebase-admin/lib/app/firebase-app.js:37:25)
    at AuthorizedHttpClient.getToken (/Applications/GenysisBuild/GenysisNest/genysis-notifications/node_modules/firebase-admin/lib/utils/api-request.js:612:34)
    at AuthorizedHttpClient.send (/Applications/GenysisBuild/GenysisNest/genysis-notifications/node_modules/firebase-admin/lib/utils/api-request.js:600:21)

based on the responses to the error messages it would appears I have a bad private_key but this can't be the case since the same private key was used to initialize firebase. Can anyone shed some light on what's going on here?

EDIT: A try/catch error around the sendAll() result in the following error:

{"library":"PEM routines","function":"get_name","reason":"no start line","code":"ERR_OSSL_PEM_NO_START_LINE"}

Firebase error messages are so far off the cause of your error which result in post like mine keep popping up.

long story short. Using the following got me over the hump: in my.env

 GOOGLE_APPLICATION_CREDENTIALS=/path/project-key.json

apparently the other recommendations from the docs only work within specific conditions. Directly using the PRIVATE_KEY will not work in all occasions and GOOGLE_APPLICATION_CREDENTIALS is a reserved name...you must use this name.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM