[Question] How to authenticate with the Microsoft Graph API from bot app?
Bot Info
- SDK Platform: Node.js
- Active Channels: Cortana
- Deployment Environment: Azure App Service
Issue Description
I have a bot service running on node.js with Cortana channel enabled. I have also enabled 'user sign-in' option at skill invocation to manage user identity. https://docs.microsoft.com/en-us/cortana/skills/configure-connected-account
Moreover, I have created an app on apps.dev.microsoft.com which has permissions to access our MS graph. When I use graph explorer in browser and login with my corp. account I can get results from following GET. https://developer.microsoft.com/en-us/graph/docs/api-reference/v1.0/api/calendar_list_events but, when I access same code from bot I get error "Bearer access token is empty.". After digging around I found we need access token to use graph Api. But, currently we are facing sign-in issue for our user as we use Azure AD as identity provider and not Microsoft identity server. Looks like Cortana can't be configured to use AAD yet.
So in this case, is there any other way to access MS graph Api in bot service? Please make a note that we can have an app which has access to ms graph and real issue is use can't sign in to Cortana due to AD vs MS AD issue otherwise we would have used following code to grab access token.
var authorizationToken = session.message.entities.find((e) => { return e.type === 'AuthorizationToken'; }); // we cant use this as user's are enable to sign in yet.
Code Example
var url = "https://graph.microsoft.com/v1.0/users/{office 365emailAddress}/calendar/events"
var meetingEvents = [];
request(url, function (error, response, body) {
if (body) {
var json = JSON.parse(body);
if (json.value) {
meetingEvents.push(json)
} else {
return;
}
}
if(error){
reject({error: error});
}
});
Expected Behavior
I would like to get API result without having to user logged in as I have an app which has access to ms graph.
+1
Finally, I was able to resolve it by having to have 2 apps. App1 - Deals with just user sign-in from AAD V1 App 2 - Has full access to graph with App permissions (not delegated) which I use to get token and call graph API.
@rjgmail88 , I am also facing same issue, what you faced earlier and didn't get any specific guidelines.
I followed below defined steps with AAD V1 and V2 always getting 401 error. It would really helpful, if you could share working sample via github .
https://docs.microsoft.com/en-us/azure/bot-service/bot-builder-tutorial-authentication?view=azure-bot-service-3.0&tabs=csharp&viewFallbackFrom=azure-bot-service-4.0