【问题标题】:How to encode a string to base 64 in the airtable javascript code editor?如何在 airtable javascript 代码编辑器中将字符串编码为 base 64?
【发布时间】:2023-01-31 02:38:53
【问题描述】:

我正在尝试从 aritable 脚本编辑器发出 api 请求。
GET 请求需要一个授权标头,其中包含身份验证字符串 api 的 Base64 摘要:USERNAME:YOUR_API_KEY。

无法使用 btoa() 在 airtable 中进行编码,因为它未定义。我检查了 SO,下面是一种不使用 btoa 进行编码的解决方案。

var Base64={_keyStr:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",encode:function(e){var t="";var n,r,i,s,o,u,a;var f=0;e=Base64._utf8_encode(e);while(f<e.length){n=e.charCodeAt(f++);r=e.charCodeAt(f++);i=e.charCodeAt(f++);s=n>>2;o=(n&3)<<4|r>>4;u=(r&15)<<2|i>>6;a=i&63;if(isNaN(r)){u=a=64}else if(isNaN(i)){a=64}t=t+this._keyStr.charAt(s)+this._keyStr.charAt(o)+this._keyStr.charAt(u)+this._keyStr.charAt(a)}return t},decode:function(e){var t="";var n,r,i;var s,o,u,a;var f=0;e=e.replace(/[^A-Za-z0-9\+\/\=]/g,"");while(f<e.length){s=this._keyStr.indexOf(e.charAt(f++));o=this._keyStr.indexOf(e.charAt(f++));u=this._keyStr.indexOf(e.charAt(f++));a=this._keyStr.indexOf(e.charAt(f++));n=s<<2|o>>4;r=(o&15)<<4|u>>2;i=(u&3)<<6|a;t=t+String.fromCharCode(n);if(u!=64){t=t+String.fromCharCode(r)}if(a!=64){t=t+String.fromCharCode(i)}}t=Base64._utf8_decode(t);return t},_utf8_encode:function(e){e=e.replace(/\r\n/g,"\n");var t="";for(var n=0;n<e.length;n++){var r=e.charCodeAt(n);if(r<128){t+=String.fromCharCode(r)}else if(r>127&&r<2048){t+=String.fromCharCode(r>>6|192);t+=String.fromCharCode(r&63|128)}else{t+=String.fromCharCode(r>>12|224);t+=String.fromCharCode(r>>6&63|128);t+=String.fromCharCode(r&63|128)}}return t},_utf8_decode:function(e){var t="";var n=0;var r=c1=c2=0;while(n<e.length){r=e.charCodeAt(n);if(r<128){t+=String.fromCharCode(r);n++}else if(r>191&&r<224){c2=e.charCodeAt(n+1);t+=String.fromCharCode((r&31)<<6|c2&63);n+=2}else{c2=e.charCodeAt(n+1);c3=e.charCodeAt(n+2);t+=String.fromCharCode((r&15)<<12|(c2&63)<<6|c3&63);n+=3}}return t}}

const stringApi = 'USER_NAME:API_KEY'
var encodedString = Base64.encode(stringApi)

let response = await fetch('https://api.com', {
    method: 'GET',
    header: {
        'Authorization': 'Basic ' + encodedString 
    }
})

然而,这仍然无法连接到 api,因为我不断从 api 收到 401 UNAUTHORIZED 响应。

我假设这种编码方法可能不正确,所以我在本地机器上使用 btoa()stringApi 进行了编码,然后将该编码的结果复制到标头中的 Authorization 中。这也没有用,因为我不断收到 401 回复。

我想知道您是否知道或有任何关于如何使用 airtable 代码编辑器正确编码为 base 64 的建议,以便我可以成功连接到服务器并停止收到 401 响应。

提前致谢!

【问题讨论】:

    标签: javascript api base64 http-status-code-401 airtable


    【解决方案1】:

    这在很多层面上都令人沮丧。够了,我有一个专门的“场景”,使用make.com返回我发送给它的任何输入的base64...

        async function btoa(inputString){
    
        // Fetch from Make
        let apiResponse = await fetch(`https://hook.us1.make.com/wa0..r?s=${inputString}`);
        let data = await apiResponse.text();
        return data.toString()
    
        }
    

    make.com 端,有btoa 函数可应用于任何输入字符串。

    【讨论】:

      猜你喜欢
      • 2013-09-26
      • 1970-01-01
      • 2012-04-10
      • 2023-03-26
      • 1970-01-01
      • 2018-03-19
      • 2017-06-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多