Step 1: Prepare your API key and secret
Visit Developer Tools page and click GET REST CREDENTIALS to get your API key and secret.
Step 2: Set your API key and secret values
Copy following script below on GAS and set your API key and secret for var PK_API_KEY
and var PK_API_SECRET
.
/* -------------------------------------- */
/* Please provide your values */
/* -------------------------------------- */
// API Key and Secret are on https://app.passkit.com/ > Developer Tools (top right menu) > REST Credentials.
var PK_API_KEY = 'your_api_key';
var PK_API_SECRET = 'your_api_secret';
function myFunction() {
// Generate PassKit auth token for an API call
var token = generateJWT(PK_API_KEY,PK_API_SECRET);
var pk_url = 'https://api.pub1.passkit.io/';
var options = {
headers: {Authorization: token, ContentType: 'application/json'}
}
// Call PassKit API. Documentation available on https://docs.passkit.io/.
// This example makes a get request to obtain an account profile.
var endpoint = pk_url + 'user/profile';
var response = UrlFetchApp.fetch(endpoint, options);
var respText = response.getContentText();
console.log(respText);
}
// Generate PassKit API token with your API key and secret
function generateJWT(key, secret) {
var body = {
"uid": key,
"exp": Math.floor(new Date().getTime() / 1000) + 3600,
"iat": Math.floor(new Date().getTime() / 1000),
"web": true,
};
header = {
"alg": "HS256",
"typ": "JWT"
};
var token = [];
token[0] = base64url(JSON.stringify(header));
token[1] = base64url(JSON.stringify(body));
token[2] = genTokenSign(token, secret);
return token.join(".");
}
function genTokenSign(token, secret) {
if (token.length != 2) {
return;
}
var hash = Utilities.computeHmacSha256Signature(token.join("."), secret);
var base64Hash = Utilities.base64Encode(hash);
return urlConvertBase64(base64Hash);
}
function base64url(input) {
var base64String = Utilities.base64Encode(input);
return urlConvertBase64(base64String);
}
function urlConvertBase64(input) {
var output = input.replace(/=+$/, '');
output = output.replace(/\+/g, '-');
output = output.replace(/\//g, '_');
return output;
}
When you run the code, you will see your account profile on the Logs.