如何从googleapis获取access_token?

问题描述

我正在尝试获取访问令牌,以用于从服务帐户访问Firebase托管API,如here所述。
下面的代码不会返回access_token,而是返回一个id_token,该ID_token在尝试使用API​​时无法通过身份验证。
我究竟做错了什么?如何获取访问令牌?

const { google } = require("googleapis");

var serviceAccount = require("../functions/src/services/serviceAccountKey.json");

async function getAccesstoken() {
    try {
        const jwtClient = new google.auth.JWT(
            serviceAccount.client_email,null,serviceAccount.private_key,["firebasehosting.googleapis.com"],null
        );
        const credentials = await jwtClient.authorize();
        console.log(credentials);
    } catch (error) {
        console.log(error);
    }
}

getAccesstoken();

它返回一个凭证对象:

{
  access_token: undefined,token_type: 'Bearer',expiry_date: undefined,id_token: '...',// edited out
  refresh_token: 'jwt-placeholder'
}

解决方法

记录下来,我终于明白了。
我的令牌范围无效:我应该使用https://www.googleapis.com/auth/firebase
有效范围已列出here