Skip to main content

Using remoteFetchAsync in a button's script

  • October 18, 2023
  • 1 reply
  • 9 views

Forum|alt.badge.img+6

I've created a script that is attached to a view's button.

In this script, I'm calling an API with remoteFetchAsync().  I need to supply basic authentication credentials, but btoa() and Buffer don't work in my script:

 

const authorization = btoa(`${ credentials.username }:${ credentials.password }`)
const authorization = Buffer.from(`${ credentials.username }:${ credentials.password }`).toString('base64')
output.text(`authorization: ${authorization}`)

Is there another option?

Is TypeScript supported in a script that is triggered by a button?  If so, how do I enable it?

FYI, the "Ask the community" link (https://community.airtable.com/c/developers/scripting) returns 404.

Alexey_Gusev
Forum|alt.badge.img+23

Hi,

I’ve found a kind of btoa function somewhere, maybe Stack Overflow, but seems like it follows different standard for char in the end

 

function encodeToBase64(str) {
    const base64Chars =
    'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
    let result = '';

    for (let i = 0; i < str.length; i += 3) {
    const group = (str.charCodeAt(i) << 16) | (str.charCodeAt(i + 1) << 8) | str.charCodeAt(i + 2);

    result +=
    base64Chars.charAt((group >> 18) & 63) +
    base64Chars.charAt((group >> 12) & 63) +
    base64Chars.charAt((group >> 6) & 63) +
    base64Chars.charAt(group & 63);
    }

    return result;
    }

then i tried to understand the encoding process and write my own.
 

    const btoa=(str,out=[])=>{  //Function splits char codes to binary, each 6 bits => char from base64
     let base64 ='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
     let fillto8=bits=>'0'.repeat(8-bits.length)+bits
     let arr=[...(str+String.fromCharCode(0).repeat(str.length%3*2%3))] //add junk to be divided by 3
     let char2bits=[...arr.map(v=>fillto8(Number(v.charCodeAt(0)).toString(2))).join('')]
     while(char2bits.length) out.push(base64[Number.parseInt(char2bits.splice(0,6).join(''),2)])
     let result=['='.repeat(out.splice(-str.length%3*2%3).length),out.join('')].reverse().join('') 
     return result
    }


one of them should work
 


Reply