我想通过firebase云函数使用google pay API,但不幸的是,nodejs在google-pay/passes-rest-samples中严重缺失,在client libraries中也不受支持。
我可以在PHP示例中测试API -即我的服务帐户已启动并链接到我的商家帐户,但我想知道如何在nodejs中使用API:
1-如何在请求调用中获取访问令牌并保存pass?
尝试了以下操作,但我总是得到401状态码:
a)使用google-auth-library-nodejs
// Create a new JWT client using the key file downloaded from the Google Developer Console
const auth = new google.auth.GoogleAuth({
keyFile: path.join(__dirname, 'serviceAccount.json'),
scopes: 'https://www.googleapis.com/auth/wallet_object.issuer',
});
const client = await auth.getClient();
const accessToken = (await client.getAccessToken()).token;
const result = (await axios.post(`https://walletobjects.googleapis.com/walletobjects/v1/loyaltyClass?strict=true`, payload,
{ headers: { Authorization: `Bearer ${accessToken}` } })
).data;b)使用jsonwebtoken
const token = jwt.sign({ payload, typ: JWT_TYPE }, credentialJson.private_key,
{
algorithm: 'RS256',
audience: AUDIENCE,
issuer: SERVICE_ACCOUNT_EMAIL_ADDRESS,
});发布于 2021-04-02 05:48:37
以下是如何使用Node.js实现这一点的example:
相关部分:
// Step 1: create a loyalty object
const loyaltyObject = await createLoyaltyObject();
// Step 2: define jwt claims
const claims = {
aud: 'google',
origins: [website],
iss: credentials.client_email,
typ: 'savetowallet',
payload: {
loyaltyObjects: [
{
id: loyaltyObject.id,
},
],
},
};
// Step 3: create and sign jwt
const token = jwt.sign(claims, credentials.private_key, { algorithm: 'RS256' });https://stackoverflow.com/questions/65431685
复制相似问题