https://discord.gg/botpress logo
OAuth tokens and variables
# 🤝help
m
Hi. I'm am a newbie to Botpress (although no stranger to programming). First up, I trust I am in the correct section to post such questions? I would like some pointers please in running an action that retrieves an OAuth token from an external server via a POST command. Question 1: ----- Part of the Action JS reads thus: const payload = { "username": "api_user", "password": "pswdsecret", "APIKey": "ABABABAB-CDCD-EFEF-ABAB-ABCDABCDABCD" }; I have setup some Workflow variables (Im not sure if this is the right scope or not, and I have replaced the value of "api_user" with {{api_variable}} which it seems to accept on the first line only, however, the other two variables below it have a red line underneath it. I'm thinking the variables work because I can change the order of these three lines and the first one always seems right. const payload = { "username": {{api_user}}, "password": {{api_pswd}}, "APIKey": {{api_key}} }; What is it in my code here that needs to be changed? Remove the commas? I presume that it is a better approach to use variables here rather than have the credentials hard-coded into the action itself? Question 2: ----- When this first section runs successfully (as a version of it works fine in Postman), I need to then use the retrieved token into a variable - which I have also declared at the Workflow scope level. The returned data is JSON which I need to extract my token from. Do I need to set anything in the Output Schema to prepare for returned data, and if so, how? I only get the options of None and Array (which doesn't expand to anything) to choose from. What am I missing here please? Any help would be appreciated. Has anybody got a script or example that does this same task? Thanks, Greg
g
in execute code cards you dont use the moustache syntax to access the variables like you do to render them in conversation context. also if you save something at the workflow scope its workflow.user, workflow.password etc. but it may be a better idea to save user data in the user scope (in execute code card its user.user, user.password etc. you also (with bp execute code card) cannot import any libraries, but it comes native with axios. if youre using an execute code card you dont need to have a schema but its best to type everything to avoid compiler warnings.
m
Hi @gentle-policeman-89141 - thankyou for your reply. I will revisit the scope of my variables and see how I go. Just out of curiousity, might you have any idea about the Output Schema and data extraction part of the question? Thankyou. Greg.
g
are you using actions or execute code card?
m
Hi @gentle-policeman-89141 - I am using Actions so far, but if Execute Card is a better method, then I'm all ears. I moved the name scope across to [user.] and it failed to populate that name variable so i have switched it back for now until I know what I'm doing wrong. Thankyou for your help.
g
ahh. i prefer execute code to be honest. but you can only use them in standard nodes. you can just type safe everything there instead of having an explicit schema