How to get the correct acces token for Firebase Dynamic Link Analytics Rest API (NodeJS)

喜欢而已 提交于 2019-12-11 04:26:26

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!