问题
I have a problem with my access_token which I get with the serveracceskey. Here is my code in NodeJS:
const admin = require('firebase-admin');
var request = require("request");
const serviceAccount = require('./serverAccountKey.json');
const credential = admin.credential.cert(serviceAccount);
credential.getAccessToken().then((accessTokenInfo) => {
const accessToken = accessTokenInfo.access_token;
const expirationTime = accessTokenInfo.expires_in;
console.log("accessToken " + accessToken );
console.log("expirationTime " +expirationTime);
var s = "Bearer " + accessToken;
request({
headers:{
'Authorization': s
},
uri:"https://firebasedynamiclinks.googleapis.com/v1/SHORTLINK/linkStats?durationDays=7",
method: "GET",
}, function(error, response, body) {
console.log(body);
});
});
and the result is like below:
{
"error": {
"code": 403,
"message": "Request had insufficient authentication scopes.",
"status": "PERMISSION_DENIED"
}
}
What am I doing wrong ? I test the link in Postman too. Something is wrong, I read all the firebase Rest API Doc.
回答1:
Admin SDK creates tokens with a specific set of scopes: https://github.com/firebase/firebase-admin-node/blob/master/src/auth/credential.ts#L272
Clearly, Dynamic Links API requires additional OAuth2 scopes in the token. You're better off using some OAuth2 library for this use case. If you were using Java or Python, Google Cloud provides libraries that handles this for you.
回答2:
I solve the problem with a helpful GitHub user. If anyone get the same problem,press this link it will be helped. https://github.com/firebase/firebase-admin-node/issues/111
来源:https://stackoverflow.com/questions/46810942/how-to-get-the-correct-acces-token-for-firebase-dynamic-link-analytics-rest-api