admin管理员组

文章数量:1432187

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)}]

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)}]
Share Improve this question asked Jun 21, 2020 at 15:37 LzcLzc 211 silver badge2 bronze badges 1
  • 1 Does this answer your question? Node.js throws "btoa is not defined" error – SMAKSS Commented Jun 21, 2020 at 15:46
Add a ment  | 

1 Answer 1

Reset to default 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.

本文标签: jwtZapier39s JavaScript function quotbtoaquot is not definedStack Overflow