About Scripting Node
This example uses password flow from RingCentral Office. This flow is only available to private apps within your own organization.
Let's create a new node to our workflow by dragging a Scripting node from the dock on to your canvas.
Then you'll want to take the following code and paste it into your Script Properties
var data = ivr.getData('AuthenticateRC'); var authResponseData = JSON.parse(data); var responseBody = JSON.parse(authResponseData.response_body); ivr.putData('$rcAccessToken', responseBody.access_token); var rcAuthorization = responseBody.token_type + ' ' + responseBody.access_token; ivr.putData('$rcAuthorization', rcAuthorization);
Your Script Properties should look like the following.
Now let's dissect each of those lines.
When you use the WWW node, the response is stored in a local model object in the IVR. To retrieve this response, we need to reference the previous WWW node by its name,
AuthenticateRC. Notice the way we do this is by using the method
getData with the prefix
var data = ivr.getData('AuthenticateRC');
What we just retrieved is just a string, but specifically, it's a JSON string. We need to convert this string into a JSON object.
var authResponseData = JSON.parse(data);
This data is all the data including headers, and all we need is the response body so let's extract that out.
var responseBody = JSON.parse(authResponseData.response_body);
Then we'll take the access token and store that access token in our IVR key/value store. Remember that IVR methods are prefixed wiht
ivr., but this time instead of getting the data, we are using
putData to put the data into the IVR key/value store.
The last step is to construct the authorization header. As we are using Password Flow, we'll need to construct an authorization string with the token type
Basic concatenated with our access token. Then we'll store that string as our Authorization string.
var rcAuthorization = responseBody.token_type + ' ' + responseBody.access_token; ivr.putData('$rcAuthorization', rcAuthorization);
Now we are ready to start invoking RingCentral Office APIs with our valid authorization header. Give it a try!