我想对android应用程序中的购买收据进行服务器端验证。我使用的是node.js后端。
我目前正在尝试使用google api validator节点模块(https://www.npmjs.org/package/google-play-purchase-validator)来完成它,这似乎是完成它的最先进的模块(执行对google的实时请求)。
在Google控制台中,我创建了一个Google帐户,然后获得了在模块中使用的电子邮件和密钥,我还将此服务帐户与我的应用程序连接,如本文Use service account to verify Google InAppPurchase中所述)。不幸的是,它不起作用。似乎节点模块生成的JWT令牌没有正确签名(我得到了以下错误:未能对jwt签名,密钥可能无效)。
下面是一些代码:
var Verifier = require('google-play-purchase-validator');
var options = {
email:'myemail@developer.gserviceaccount.com',
key: 'myprivatekey',
};
var verifier = new Verifier(options);
verifier.verify(item.Receipt, function cb(err, response) {
if (err) {
console.log("there was an error validating the receipt");
console.log(err);
}
else{
console.log("sucessfully validated the receipt");
console.log(response);
}私钥来自使用以下命令从谷歌提供的.pem文件中生成的.p12文件:
openssl pkcs12 -in downloaded-key-file.p12 -out your-key-file.pem -nodesgoogle购买-验证器模块基于其他一些模块(,request,crypto)。我试着调试了一下,一切似乎都做对了。
知道我哪里错了吗?
保罗
发布于 2014-11-17 09:47:18
刚刚添加了有关如何向模块的Readme.md获取正确凭据的说明。
https://www.npmjs.org/package/google-play-purchase-validator
或者在这里找到他们:
发布于 2020-10-09 07:02:12
这个答案只有在你遵循上述公认答案的步骤后才会有用。我正在使用谷歌跳跃式npm软件包,以验证消费购买。
import { google } from 'googleapis';
const auth = new google.auth.GoogleAuth({
credentials: {
client_email: 'email from json file',
private_key:
'private key from json file',
},
scopes: ['https://www.googleapis.com/auth/androidpublisher'],
});
const authClient = await auth.getClient();
google.options({ auth: authClient });
try {
// packageName,productId,token you can get from request sent from android
const purchaseResponse: AndroidPurchaseResponse = await google
.androidpublisher({
version: 'v3',
}).purchases.products.get({
packageName: 'packageName',
productId: 'productId',
token: 'purchaseToken',
});
if (purchaseResponse.data.purchaseState !== 0) {
throw new BadRequestException('Purchase is either Pending or Cancelled!');
}
if (purchaseResponse.data.consumptionState !== 0) {
throw new BadRequestException('Purchase is already consumed!');
}
if (purchaseResponse.data.orderId !== requestDto.orderId) {
throw new BadRequestException('Invalid orderId');
}
return purchaseResponse;
} catch (e) {
throw new BadRequestException(e);
}https://stackoverflow.com/questions/26941195
复制相似问题