Integration

Microsoft Outlook 365 Integration with Salesforce Using REST API with Example

Microsoft Outlook 365 Integration with Salesforce Using REST API with Demo

Outlook and Salesforce Integration

Here is the demo of the same built on Salesforce using Salesforce Sites. (https://msofficeintegration-developer-edition.ap5.force.com/)

In this post, we will learn how to connect Salesforce with Outlook 365 using REST API.

For this, the first step is to create an Application in the “Microsoft App Registration Portal” (https://apps.dev.microsoft.com/). Log in using your office 365 account credentials.

Click on Add an App button to start creating the application. Enter the Application Name and click create.  Now do as mentioned in the snapshot below:


Save the changes.



Once done create a URL in the format below:

https://login.microsoftonline.com/common/oauth2/v2.0/authorize?response_type=id_token+token&client_id=c68aa37b-07a9-42f9-bc81-78cbcf49bda5&redirect_uri=https://msofficeintegration-developer-edition.ap5.force.com&scope=openid+profile+User.Read+Mail.Read+Mail.ReadWrite+Calendars.Read+Calendars.ReadWrite+Contacts.ReadWrite&state=f175f48d-d277-9893-9c8d-dcc2a95ffe16&nonce=c68aa37b-07a9-42f9-bc81-78cbcf49bda5&response_mode=fragment


Replace below values:
client_id value with your application Client ID.
redirect_uri with the page where you would like the user to redirect after authorization.
nonce should be same as your client id.

once the user authenticates on the above link he will be redirected to the URL you have configured in the app along with the #access_token 

This access token now can be used to send REST call to Microsoft to fetch details. 
Microsoft graph API's details and examples can be explorer using Graph Explorer. (https://developer.microsoft.com/en-us/graph/graph-explorer).


Sample code for fetching latest emails
HttpRequest request = new HttpRequest();
        request.setEndpoint('https://graph.microsoft.com/v1.0/me/MailFolders/inbox/messages');
        request.setMethod('GET');
        AuthoriztionValue= 'bearer ' + Access_token; returned after Authorization
        request.setHeader('Authorization', AuthoriztionValue);
        request.setHeader('Accept', 'text/html, image/gif, image/jpeg, application/json');  
        request.setHeader('Prefer', 'outlook.allow-unsafe-html');
        String responsebody = (new Http()).send(request).getBody();
        
        system.debug('*****response*******'+responsebody);
        RespBody= responseBody;

Here is the demo of the same built on Salesforce using Salesforce Sites. (https://msofficeintegration-developer-edition.ap5.force.com/)

About Saurabh Dua

0 comments:

Post a Comment

Powered by Blogger.