How to authenticate SharePoint Online without a user in Dialogue Studio
Accessing SharePoint Online REST API requires an access token. This guide will tell you how to achieve this. In this example we will be looking up a contact.
Prerequisites
-
SharePoint App ClientID and ClientSecret available, generated during installation UCC A Unified Contact Center, or UCC, is a queue of interactions (voice, email, IM, etc.) that are handled by Agents. Each UCC has its own settings, IVR menus and Agents. Agents can belong to one or several UCCs and can have multiple skills (competencies). A UCC can be visualized as a contact center “micro service”. Customers can utilize one UCC (e.g. a global helpdesk), a few UCC’s (e.g. for each department or regional office) or hundreds of UCC’s (e.g. for each bed at a hospital). They are interconnected and can all be managed from one central location. Creator. Learn More
Get an access
First step is generating access token. We start with a function node.
Steps:
-
Drag and drop a Function node.
-
Open Function node.
-
Enter the following function:
Note: Replace the following:-
[TenantID] = Directory (Tenant) ID
-
[ClientID] = SharePoint App Client ID
-
[ClientSecret] = SharePoint App Client Secret, Make sure ULR encode.
https://www.url-encode-decode.com/ -
[Resource] = yourtenantname.sharepoint.com
CopyFunctionmsg.credentials =
{
"realm": "[TenantID]",
"client_id":"[ClientID]",
"client_secret":"[ClientSecret]",
"resource":"[Resource]",
"principal" : "00000003-0000-0ff1-ce00-000000000000"
}
msg.payload = "grant_type=client_credentials&client_id=" + msg.credentials.client_id + "@" + msg.credentials.realm + "&client_secret=" + msg.credentials.client_secret + "&resource=" + msg.credentials.principal + '/' + msg.credentials.resource + "@" + msg.credentials.realm
return msg; -
Now that we prepared our message we can send the request. This is done using a HTTP Request node.
Steps:
-
Drag and drop a http request node.
-
Open Node:
-
Method = Post
-
URL = https://accounts.accesscontrol.windows.net/[TenantID]/tokens/OAuth/2
Note: Replace [TenantID] with Directory (Tenant) ID -
Return = a parsed JSON object
-
-
Connect the end of the Function node to the begin of the HTTP request node.
Next we want to check if our output was successful. This can be done with a Switch node.
Steps:
-
Drag and drop a Switch Node.
-
Open Node:
-
Change Property to msg.statusCode.
-
Add an option for a OK response.
== 200
-
Add the option otherwise for any other response.
-
-
Connect end of the HTTP request node to begin of the Switch Node.
If the response was 200 OK then we can store the tokens. Note that an access token is only valid for one hour, for that reason we also store the expiration time. This can be done with a Function node.
Steps:
-
Drag and drop a Function node.
-
Open Function node.
-
Enter the following function:
CopyFunctionvar access_token = msg.payload.access_token
flow.set('at', access_token);
var date = new Date();
msg.date = new Date();
flow.set('expire', new Date(date.setHours(date.getHours()+1)));
return msg; -
Connect "== 200" of the Switch node to begin of the Function node.
Now we have our token and we can start using it.
Call SharePoint Online with the access token
With our tokens in memory we can call Dynamics 365 Before we start we need to check if the token is still active. We will do this with a Function node.
Steps:
-
Drag and drop a Function node.
-
Open Function node.
-
Enter the following function:
Next we need to check if our expire time is not past the current time. This is done with a Switch node.
Steps:
-
Drag and drop a Switch Node.
-
Open Node:
-
Change Property to flow.expire
-
Add an option for if not expired
>= msg.date
-
Add the option otherwise for expired
-
-
Connect end of the Function node to begin of the Switch Node.
We are going to continue on the not expired flow. The expired flow will loop back to chapter Request an access token.
Our next step is to prepare the request, this is done using a Function node.
Steps:
-
Drag and drop a Function node.
-
Open Function node.
-
Enter the following function:
CopyFunctionmsg.headers = {
"Authorization": "Bearer " + flow.get('at'),
"Accept": "application/json",
};
return msg; -
Connect ">= msg.date" of the Switch node to begin of the Function node.
Now that we prepared our message we can send the request. This is done using a HTTP Request node.
Steps:
-
Drag and drop a http request node.
-
Open Node:
-
Method = Post
-
URL = Example url to get all agent uri's:
CopyURLhttps://[TenantName].sharepoint.com/[PathUccSite]/_api/web/lists/getbytitle('Agents')/items?$select=wsp_ucc_Agent
Note: Replace the following:-
[TenantName] = Your Tenant name, example : contoso
-
[PathUccSite] = For example: sites/ucc/uccsupport
-
-
Return = a parsed JSON object
-
-
Connect the end of the Function node to the begin of the HTTP request node.
The output is stored in msg.payload and can be used in the rest of your flow. You can use the following expression to greet the customer by name: