2

I am trying to make use of existing sample code for PassKit to create a JWT token with "Run JavaSript by Zapier". But the Zapier does not recognized the btoa function.

ReferenceError: btoa is not defined

According to this website, this function can be called directly. Any idea?

var b64data = btoa("this is my string to turn into base64");

Below is the code that I have written.

var Zap = {
  base64url:function(input){
    var base64String = btoa(input); //<--error here
    return urlConvertBase64(base64String);
  },
  urlConvertBase64:function(input){
    var output = input.replace(/=+$/, '');
    output = output.replace(/\+/g, '-');
    output = output.replace(/\//g, '_');
    return output;
  },
  generateJWT:function(key){
    var header = {
      "alg": "HS256",
      "typ": "JWT"
    };
    var time_now = Math.floor(new Date().getTime()/1000);
    var exp = time_now + 30;
    var body = {
      "exp": exp,
      "key": key
    };
    var token = [];
    token[0] = Zap.base64url(JSON.stringify(header));
    return body;
  }
};
output = [{body: Zap.generateJWT(inputData.Api_key)}]
1

1 Answer 1

7

You can use the following:

var b64data = Buffer.from('this is my string to turn into base64').toString('base64');

Node.js doesn't seem to support the atob() and btoa() methods.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.