eastern-cyan
eastern-cyan17mo ago

Let's Solve OAuth2.0 Authentication in Voiceflow Together!

Hi everyone, I need to use an external API that requires OAuth2.0 authentication. Can someone guide me on how to set up a function or something to get an access token? I need to pass my 'client_id' and 'client_secret'. Any examples or best practices would be appreciated!
9 Replies
Tico | Voiceflow Assistant
To set up OAuth2.0 authentication in Voiceflow, you'll need to create a function that can retrieve an access token from the API. Here's a step-by-step guide on how to do this: 1. Create a new function in your Voiceflow project: In your Voiceflow project, navigate to the 'Functions' tab on the left side of the screen. Click on '+ New Function' to create a new function. 2. Write your function: In the function editor, you'll need to write a function that makes a POST request to the API's token endpoint. Here's an example of what this might look like in JavaScript:
const fetch = require('node-fetch');

async function getAccessToken() {
const response = await fetch('https://api.example.com/token', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: `grant_type=client_credentials&client_id=YOUR_CLIENT_ID&client_secret=YOUR_CLIENT_SECRET`,
});

const data = await response.json();

return data.access_token;
}
const fetch = require('node-fetch');

async function getAccessToken() {
const response = await fetch('https://api.example.com/token', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: `grant_type=client_credentials&client_id=YOUR_CLIENT_ID&client_secret=YOUR_CLIENT_SECRET`,
});

const data = await response.json();

return data.access_token;
}
Replace 'https://api.example.com/token' with the token endpoint for your API. Replace 'YOUR_CLIENT_ID' and 'YOUR_CLIENT_SECRET' with your actual client ID and client secret. 3. Use the function in your project: Once you've written your function, you can use it in your project to retrieve an access token. To do this, add a 'Code' block to your project, and call your function from within the 'Code' block. Remember, the function above is a basic example and might need to be adjusted based on the specific requirements of the API you're using. Always refer to the API's documentation for the correct endpoints and parameters to use. Also, keep in mind that the access token you receive may expire after a certain period of time. If this is the case, you'll need to call your function again to get a new access token when the old one expires.
eastern-cyan
eastern-cyanOP17mo ago
Node-fetch is not supported in VoiceFlow This is in your example const fetch = require('node-fetch'); is there another method
equal-aqua
equal-aqua17mo ago
Hi all, my first post here so it is nice to meet you all and thank you for any help in advance! I am building a chatbot that can pull data (a question, 3 answer options and the correct answer) from a Google Sheets via Zapier to ask the chatbot user. I have set up the API POST request on Voiceflow as shown in Screenshot 1, entered my CURL request in Screenshot 2, but when I use sample data in Screenshot 3 it doesn't appear that Voiceflow is capturing any of the variable inputs in the test request in Screenshot 4 despite receiving a 200 OK message. Please could someone advise what I am doing wrong? Alternatively, please direct me to any examples of successful API POST requests where Voiceflow is pulling data from an external source and I will try and replicate this. (I appreciate my question is quite complex but I have tried everything I could and hit a standstill!)
No description
No description
No description
No description
W. Williams (SFT)
You can use fetch within Functions inside VF
eastern-cyan
eastern-cyanOP17mo ago
@W. Williams (SFT) Do you have an example wouldn’t mind sharing . The code above is for a function but
W. Williams (SFT)
@Mark Why are you just not calling the Zappier endpoint?
eastern-cyan
eastern-cyanOP17mo ago
@JAPI I believe that response above is meant for you . I figured it out. If anyone else is new to Vocieflow and learning it here is a video of how I'm doing it. Hope it helps. https://youtu.be/cOzGOR4rgRQ?si=VJOZDhMXptbo9taB
equal-aqua
equal-aqua16mo ago
I could only see the Zapier endpoint as an option in the trigger step on Zapier and the action I am after will occur further down the Zap chain where I can't see a Zapier endpoint URL to use. I actually watched your video yesterday as I am alternatively trying to use the Google Sheets API directly (rather than go through Zapier) which incurs the OAuth2.0 issue and found it really informative, but me having no coding experience I am struggling to replicate your Lambda segment so if you have any suggestions/tips for no coders then please do share!
eastern-cyan
eastern-cyanOP16mo ago
@JAPI , I got you bro, DM me. we could setup a Google Meets. I'll walk you through the steps.

Did you find this page helpful?