我正尝试使用Firebase云功能和firebase-admin组合来为向auth中的用户添加auth声明的路径。 我正在用firebase serve对其进行测试,并且尝试使用以下代码进行此操作:
<script>
var myreset = [21,37,00]; // at what time to reset - 19:40:00
var mycountdown = startCountdown();
function startCountdown(){
var enddate = calculateEndDate();
return setInterval(function(){tickTock(calculateStartDate(),enddate)}, 1000);
}
function calculateStartDate(){ //this needs to be edited if using the server time
return new Date();
}
function calculateEndDate(){
var enddate = new Date();
enddate.setHours(myreset[0]);
enddate.setMinutes(myreset[1]);
enddate.setSeconds(myreset[2]);
return enddate;
}
function tickTock(startdate, enddate){
var diff = enddate.getTime() - startdate.getTime();
d = diff >= 0 ? diff : diff + 24*3600*1000;
var h = Math.floor(d / 3600 / 1000);
var m = Math.floor(d / 60 / 1000) - 60*h;
var s = Math.floor(d / 1000) - 3600*h - 60*m;
printCountdown(h,m,s);
}
function pluralize(word,count){
return (count > 1) ? word+'s ' : word+' ';
}
function printCountdown(h,m,s){
var t = h + pluralize(' hour',h)+ m+pluralize(' minute',m)+ s + pluralize(' and second',s);
$('#mycountdown').html(t);
}
</script>
那实际上不起作用,并且给了我下面的错误:
const functions = require('firebase-functions');
const admin = require("firebase-admin");
admin.initializeApp(functions.config().firebase);
const func = () => {
admin.auth().setCustomUserClaims("(req.params.uid", {isActivated: true}).then(() => {
console.log("Done")
})
.catch(e => console.error(e))
}
setTimeout(func, 4000)
// // Create and Deploy Your First Cloud Functions
// // https://firebase.google.com/docs/functions/write-firebase-functions
//
exports.api = functions.https.onRequest((request, response) => {
console.log(functions.config())
response.send("Hello from Firebase!");
});
有一篇博客文章说,如果我想通过firebase-admin使用除firestore和实时数据库以外的任何东西,我必须做一些额外的配置,所以我尝试通过使用以下代码来测试firestore是否正常工作:>
{ Error: Credential implementation provided to initializeApp() via the "credential" property failed to fetch a valid Google OAuth2 access token with the following error: "Error fetching access token: Error while making request: getaddrinfo ENOTFOUND metadata.google.internal metadata.google.internal:80. Error code: ENOTFOUND".
> at FirebaseAppError.FirebaseError [as constructor] (C:\Users\jishn\WorkSpace\Works\Status\firebase\functions\node_modules\firebase-admin\lib\utils\error.js:42:28)
> at FirebaseAppError.PrefixedFirebaseError [as constructor] (C:\Users\jishn\WorkSpace\Works\Status\firebase\functions\node_modules\firebase-admin\lib\utils\error.js:88:28)
> at new FirebaseAppError (C:\Users\jishn\WorkSpace\Works\Status\firebase\functions\node_modules\firebase-admin\lib\utils\error.js:123:28)
> at C:\Users\jishn\WorkSpace\Works\Status\firebase\functions\node_modules\firebase-admin\lib\firebase-app.js:121:23
> at process._tickCallback (internal/process/next_tick.js:68:7)
> errorInfo:
> { code: 'app/invalid-credential',
> message:
> 'Credential implementation provided to initializeApp() via the "credential" property failed to fetch a valid Google OAuth2 access token with the following
error: "Error fetching access token: Error while making request: getaddrinfo ENOTFOUND metadata.google.internal metadata.google.internal:80. Error code: ENOTFOUND".' },
> codePrefix: 'app' }
再次给我一个与管理员凭据本身有关的错误:
// The Cloud Functions for Firebase SDK to create Cloud Functions and setup triggers.
const functions = require('firebase-functions');
// The Firebase Admin SDK to access the Firebase Realtime Database.
const admin = require('firebase-admin');
admin.initializeApp();
exports.api = functions.https.onRequest((req, res) => {
var db = admin.firestore();
db.collection('users').get()
.then(snapshot => {
snapshot.forEach(doc => {
console.log(doc.id, '=>', doc.data());
});
})
.catch(err => {
console.log('Error getting documents', err);
});
res.send("Hi")
});
和Error getting documents Error: Could not load the default credentials. Browse to https://cloud.google.com/docs/authentication/getting-started for more information.
> at GoogleAuth.getApplicationDefaultAsync (C:\Users\jishn\WorkSpace\Works\Status\firebase\functions\node_modules\google-auth-library\build\src\auth\googleauth.js:159:19)
> at process._tickCallback (internal/process/next_tick.js:68:7)
返回functions.config()
无论发生什么情况,命令firebase服务都处于关闭状态。 下面的代码适用于serviceAccount.json文件:
{}
但是我认为必须对云功能使用这种管理员初始化并不是一个好主意。
答案 0 :(得分:0)
从Github问题解决:
通过一些老问题,我相信这在#2214的8.5.0版本中已解决
解决方案如下:
const admin = require('firebase-admin');
admin.initializeApp(); // no neeed for functions.config().firebase