如何使用 Nodejs 将通行证保存到 Google 支付?
How to Save Passes to Google Pay using Nodejs?
我想用google pay API for pass with firebase cloud functions,可惜nodejs在google-pay/passes-rest-samples and is not supported in the client libraries.
我能够测试 PHP 示例中的 API - 即我的服务帐户已启动并链接到我的商家帐户,但我想知道如何使用 API 在 nodejs 中:
1-如何获取访问令牌并在请求调用中保存通行证?
尝试了以下方法,但我总是收到 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,
});
这里是 example 如何使用 Node.js 实现此目的的方法:
相关部分:
// 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' });
我想用google pay API for pass with firebase cloud functions,可惜nodejs在google-pay/passes-rest-samples and is not supported in the client libraries.
我能够测试 PHP 示例中的 API - 即我的服务帐户已启动并链接到我的商家帐户,但我想知道如何使用 API 在 nodejs 中:
1-如何获取访问令牌并在请求调用中保存通行证?
尝试了以下方法,但我总是收到 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,
});
这里是 example 如何使用 Node.js 实现此目的的方法:
相关部分:
// 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' });