最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to create Firebase Authentication custom token without service account json in nodejs? - Stack Overflow

programmeradmin4浏览0评论

I want to create a custom token without using a service account json. Tried the below config items:

and used the below code to generate a token

admin.auth().createCustomToken(uid)
  .then(function(customToken) {
    // Send token back to client
  })
  .catch(function(error) {
    console.log('Error creating custom token:', error);
  });

Getting the below error :

'Failed to determine service account. Make sure to initialize the SDK with a service account credential. Alternatively specify a service account with iam.serviceAccounts.signBlob permission. Original error: Error: Error while making request: getaddrinfo ENOTFOUND metadata metadata:80. Error code: ENOTFOUND'

Used the latest "firebase-admin" npm module.

Kindly advice.

I want to create a custom token without using a service account json. Tried the below config items: https://firebase.google./docs/auth/admin/create-custom-tokens#using_a_service_account_id

https://firebase.google./docs/auth/admin/create-custom-tokens#letting_the_admin_sdk_discover_a_service_account

and used the below code to generate a token

admin.auth().createCustomToken(uid)
  .then(function(customToken) {
    // Send token back to client
  })
  .catch(function(error) {
    console.log('Error creating custom token:', error);
  });

Getting the below error :

'Failed to determine service account. Make sure to initialize the SDK with a service account credential. Alternatively specify a service account with iam.serviceAccounts.signBlob permission. Original error: Error: Error while making request: getaddrinfo ENOTFOUND metadata metadata:80. Error code: ENOTFOUND'

Used the latest "firebase-admin" npm module.

Kindly advice.

Share Improve this question edited Jun 19, 2019 at 18:06 Doug Stevenson 318k36 gold badges454 silver badges472 bronze badges asked Jun 19, 2019 at 18:02 NadhasNadhas 5,8072 gold badges30 silver badges45 bronze badges 1
  • Unless your code is running in a Google managed environment (Cloud Function, etc) the only way you can use is the service account JSON file. – Herohtar Commented Jun 19, 2019 at 18:09
Add a ment  | 

4 Answers 4

Reset to default 6

If your code is not running in a managed runtime in Google Cloud, you must either provide a service account JSON file or at very least the serviceAccountId app option.

admin.initializeApp({
  serviceAccountId: '[email protected]',
});

2022 Answer

In the latest firebase-admin version, simply providing serviceAccountId won't work. You also need to specify projectId:

admin.initializeApp({
  projectId: process.env.PROJECT_ID,
  serviceAccountId: process.env.SERVICE_ACCOUNT_ID,
})

If you don't, you will get the following error:

  errorInfo: {
    code: 'app/invalid-credential',
    message: 'Failed to determine project ID: Error while making request: getaddrinfo ENOTFOUND metadata.google.internal. Error code: ENOTFOUND'
  },

You should initialize admin by passing your service account key(Download it from Firebase) to credential first. Use this code before calling admin.auth().

var serviceAccount = require('./ServiceAccountKey.json')

admin.initializeApp({
    credential: admin.credential.cert(serviceAccount)
});

Short answer: You cannot, the sdk key is the only way to authenticate and authorize your app in order to grant permissions.

Long (not that long) answer: use a cloud function, if it is into your project, it will init it for you, so you don't have to use the key to instantiate the sdk

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论