I want to use google pay API for passes with firebase cloud functions, but unfortunately, nodejs is bitterly missed in google-pay/passes-rest-samples and is not supported in the client libraries.
I was able to test the API in the PHP sample - that is my service account is up and linked to my merchant account, but I want to know how to use the API in nodejs:
1- How to get an access token and save a pass in the request call?
Tried the following but I'm always getting 401 status code:
a) Using 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) Using jsonwebtoken
const token = jwt.sign({ payload, typ: JWT_TYPE }, credentialJson.private_key,
{
algorithm: 'RS256',
audience: AUDIENCE,
issuer: SERVICE_ACCOUNT_EMAIL_ADDRESS,
});
Here is an example of how to achieve this with Node.js:
The relevant parts:
// 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' });