firebase功能的服务帐户身份验证访问Google API(针对应用内购买信息)失败

Yuy*_*hou 11 google-api node.js google-play-developer-api android-inapp-purchase google-cloud-functions

我们正在准备一个firebase触发器来处理Android的实时开发人员通知,我们必须使用Google Play Developer API来了解用户订阅的详细信息.因此,我们在Google Play控制台中关联了firebase服务帐户,并授予了必要的访问权限.

我们使用谷歌的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);
  });
});
Run Code Online (Sandbox Code Playgroud)

但它返回一个错误状态:'无效的凭据',我对此有点失落.我已经使用curl测试了API和授权令牌,它也显示了401错误响应.

我不确定我的流程是否正确,有人能给我任何建议吗?

evg*_*ein 6

随着JWTgoogle-auth-library谷歌示例

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);
});
Run Code Online (Sandbox Code Playgroud)

data包含productIdtoken并且packageName