Firebase 函数访问权限 Google API(用于应用内购买信息)的服务帐号身份验证失败



我们正在准备一个Firebase触发器来处理Android的实时开发者通知,我们必须使用Google Play开发者API来了解用户订阅的详细信息。因此,我们在 Google Play 管理中心内关联了 Firebase 服务帐号,并授予了必要的访问权限。

我们使用google的nodejs库用于googleapis(github链接(,并编写了一段这样的代码:

return google.auth.getClient({
keyFile: path.join(__dirname, './service-account.json'),
scope: 'https://www.googleapis.com/auth/androidpublisher'
}) .then((client) => {
const androidpublisher = google.androidpublisher({
version: 'v3',
auth: client
});
const params = {
packageName: ANDROID_PACKAGE_NAME,
subscriptionId: ANDROID_SUBSCRIPTIONID,
token: token
};
return androidpublisher.purchases.subscriptions.get(params)
.then(res => {
console.log(`The response is ${res.data}`);
return null;
})
.catch(error => {
console.error(error);
});
});

但它向我们返回了一个错误状态:"凭据无效",我对此有点迷茫。我已经使用带有授权令牌的 curl 该 API 进行了测试,它也显示了 401 错误响应。

我不确定我上面的流程是否正确,任何人都可以为我提供任何建议吗?

google-auth-library中的JWT.谷歌样本

const googleapis = require('googleapis');
const { JWT } = require('google-auth-library');
const serviceAccount = require('./serviceAccountKey.json');
const getAuthorizedClient = () => new JWT({
email: serviceAccount.client_email,
key: serviceAccount.private_key,
scopes: ['https://www.googleapis.com/auth/androidpublisher']
});
const getAndroidpublisher = () => googleapis.google.androidpublisher({
version: 'v3',
auth: getAuthorizedClient()
});
const requestProductValidation = data => new Promise((resolve, reject) => {
getAndroidpublisher().purchases["products"].get(data, (err, response) => {
if (err) {
console.log(`The API returned an error: ${err}`);
resolve({status: "Error"});
} else {
const isValid = response && response.data && response.data.purchaseState === 0;
resolve({status: isValid ? "OK" : "Error"});
}
});
});
exports.purchaseValidationAndroid = functions.https.onCall((data, context) => {
return requestProductValidation(data);
});

data包含productIdtokenpackageName

相关内容

最新更新