如何获取Firebase Dynamic Link Analytics Rest API(NodeJS)的正确acces令牌 [英] How to get the correct acces token for Firebase Dynamic Link Analytics Rest API (NodeJS)

查看:94
本文介绍了如何获取Firebase Dynamic Link Analytics Rest API(NodeJS)的正确acces令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的server_eskey出现了我的access_token问题.这是我在NodeJS中的代码:

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);
    });
});

结果如下:

{
    "error": {
        "code": 403,
        "message": "Request had insufficient authentication scopes.",
        "status": "PERMISSION_DENIED"
    }
}

我在做什么错?我也在Postman中测试了链接.出了点问题,我阅读了所有的firebase Rest API文档.

What am I doing wrong ? I test the link in Postman too. Something is wrong, I read all the firebase Rest API Doc.

推荐答案

Admin SDK创建具有一组特定范围的令牌:

Admin SDK creates tokens with a specific set of scopes: https://github.com/firebase/firebase-admin-node/blob/master/src/auth/credential.ts#L272

很明显,动态链接API需要令牌中的其他OAuth2范围.在这种情况下,最好使用一些OAuth2库.如果您使用的是Java或Python,Google Cloud会为您提供处理此问题的库.

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.

这篇关于如何获取Firebase Dynamic Link Analytics Rest API(NodeJS)的正确acces令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆