node.js - Firebase 功能访问 Google API(用于应用内购买信息)的服务帐户身份验证失败

标签 node.js google-api google-cloud-functions google-play-developer-api android-inapp-purchase

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

我们使用 google 的 nodejs 库为 googleapis ( github link ) 写了一段这样的代码:

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

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

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

最佳答案

使用来自 google-auth-libraryJWTGoogle sample

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

关于node.js - Firebase 功能访问 Google API(用于应用内购买信息)的服务帐户身份验证失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51982397/

相关文章:

javascript - ESLint发现太多警告(最大值:0),并且我无法在firebase函数上部署

javascript - 如何计算 Node.js 包的加载时间损失?

javascript - 减少包含对象而不是整数的多维数组

node.js - npm 安装 Fabric-ca-client 失败

PHP:调用 Google Play Developer API 中未定义的方法

python : Get value from a list of Json dictionary

google-cloud-platform - Google Cloud Function Gen 1 部署失败

node.js - 从路由器传递值到 Node js中查看

javascript - Node.js 和 Comet

google-api - 如何增加 Google Translate API 的配额?