好的-关于我最初的问题的技巧与获取用于 api 调用的访问令牌有关。
const { JWT } = require('google-auth-library');
function getJWTResultWithAccessAndRefreshToken(jsonObjectFromGoogleKeyEtcFile,
callbackWithErrAndResult) {
var scopes = [
"https://www.googleapis.com/auth/bigquery",
"https://www.googleapis.com/auth/cloud-platform",
"https://www.googleapis.com/auth/devstorage.full_control",
"https://www.googleapis.com/auth/devstorage.read_only",
"https://www.googleapis.com/auth/devstorage.read_write"
];
var jwt = new JWT(
jsonObjectFromGoogleKeyEtcFile.client_email,
null,
jsonObjectFromGoogleKeyEtcFile.private_key,
scopes);
jwt.authorize(function (err, result) {
callbackWithErrAndResult(err, result.access_token, result.refresh_token);
});
}
在这里,jsonObjectFromGoogleKeyEtcFile 是您在 Google Cloud Platform APIs & Services 页面中生成“服务帐户密钥”/凭据时获得的 json 文件中的 json 对象。
生成的 access_token 可用于进行如下调用 - 这很有效 - 我使用了上面函数中的 access_token,并从 jsonObjectFromGoogleKeyEtcFile 的 project_id 属性中获取了 projectId:
curl -H "Authorization: Bearer generated_via_jwt_access_token" \
https://www.googleapis.com/bigquery/v2/projects/projectId/datasets
有趣的是,你也会得到一个 refresh_token,但它的值是“jwt-placeholder”
唷。