page_type | description | products | languages | |||
---|---|---|---|---|---|---|
sample |
This sample demonstrates how to use the Microsoft Graph .NET SDK to access data in Office 365 from Bot Framework bots. |
|
|
This sample demonstrates how to use the Microsoft Graph .NET SDK to access data in Office 365 from Microsoft Bot Framework bots.
NOTE: This sample was originally built from a tutorial published on the Microsoft Graph tutorials page. That tutorial has been removed.
To run the completed project in this folder, you need the following:
- The .NET Core SDK 6.x installed on your development machine.
- Bot Framework Emulator
- ngrok
- Either a personal Microsoft account with a mailbox on Outlook.com, or a Microsoft work or school account with an Exchange Online mailbox.
- An Azure subscription and an account with the Application developer, Application administrator, Cloud application administrator, or Global administrator role.
If you don't have a Microsoft account, there are a couple of options to get a free account:
- You can sign up for a new personal Microsoft account.
- You can sign up for the Microsoft 365 Developer Program to get a free Office 365 subscription.
If you don't have an Azure subscription, you can create an account for free.
-
Open a browser and navigate to the Azure Portal. Login using the account associated with your Azure subscription.
-
Select the upper-left menu, then select Create a resource.
-
On the New page, search for
Azure Bot
and select Azure Bot. -
On the Azure Bot page, select Create.
-
Fill in the required fields. The Bot handle field must be unique. Be sure to review the different pricing tiers and select what makes sense for your scenario. If this is just a learning exercise, you may want to select the free option.
-
For Type of App, select Multi Tenant.
-
For Creation type, select Create new Microsoft App ID.
-
Select Review + create. Once validation completes, select Create.
-
Once deployment has finished, select Go to resource.
-
Under Settings, select Configuration. Select the Manage link next to Microsoft App ID.
-
Select New client secret. Add a description and choose an expiration, then select Add.
-
Copy the client secret value before you leave this page. You will need it in the following steps.
[!IMPORTANT] This client secret is never shown again, so make sure you copy it now. You will need to enter this value in multiple places so keep it safe.
-
Select Overview in the left-hand menu. Copy the value of the Application (client) ID and save it, you will need it in the following steps.
-
Return to the home page of the Azure portal, then select Azure Active Directory.
-
Select App registrations.
-
Select New registration. On the Register an application page, set the values as follows.
- Set Name to
Graph Calendar Bot Auth
. - Set Supported account types to Accounts in any organizational directory and personal Microsoft accounts.
- Under Redirect URI, set the first drop-down to
Web
and set the value tohttps://token.botframework.com/.auth/web/redirect
.
NOTE:
https://token.botframework.com/.auth/web/redirect
is the default Bot Framework OAuth redirect URL for the public Azure cloud with no data residency requirements. Depending on your environment, you may need to use a different redirect URL. See OAuth URL support in Azure Bot Service for more information. - Set Name to
-
Select Register. On the Graph Calendar Bot Auth page, copy the value of the Application (client) ID and save it, you will need it in the following steps.
-
Select Certificates & secrets under Manage. Select the New client secret button. Enter a value in Description and select one of the options for Expires and select Add.
-
Copy the client secret value before you leave this page. You will need it in the following steps.
-
Select API permissions, then select Add a permission.
-
Select Microsoft Graph, then select Delegated permissions.
-
Select the following permissions, then select Add permissions.
- openid
- profile
- Calendars.ReadWrite
- MailboxSettings.Read
-
Create a new file in the GraphCalendarBot directory named appsettings.Development.json and add the following code.
{ "MicrosoftAppId": "YOUR_BOT_APP_ID_HERE", "MicrosoftAppPassword": "YOUR_BOT_PASSWORD_HERE" }
-
Replace
YOUR_BOT_APP_ID_HERE
with the Application (client) ID of your bot. (Step 13 of Create an Azure Bot in Azure portal) -
Replace
YOUR_BOT_PASSWORD_HERE
with the client secret of your bot. (Step 12 of Create an Azure Bot in Azure portal)
You can run this sample from your command-line interface (CLI), Visual Studio Code, or Visual Studio.
-
In your CLI, navigate to the GraphCalendarBot directory.
-
Run the bot with the following command.
dotnet run
- In Visual Studio Code, select File, then Open Folder....
- Browse to the folder where you cloned this repository and select Select Folder.
- Select Run, then Start Debugging. (or press F5)
- Open the GraphCalendarBot.csproj file in Visual Studio.
- Press
F5
to run the project.
-
Open the Bot Framework Emulator. Select the gear icon ⚙ on the bottom left.
-
Enter the local path to your installation of ngrok, and enable the Bypass ngrok for local addresses and Run ngrok when the Emulator starts up options.
-
Enable the Use a sign-in verification code for OAuthCards option. Select Save.
-
Select the File menu, then New Bot Configuration....
-
Fill in the fields as follows.
- Bot name:
CalendarBot
- Endpoint URL:
http://localhost:3978/api/messages
- Microsoft App ID: the application ID of your Graph Calendar Bot app registration
- Microsoft App password: your Graph Calendar Bot client secret
- Encrypt keys stored in your bot configuration: Enabled
- Bot name:
-
Select Save and connect. After the emulator connects, you should see
Welcome to Microsoft Graph CalendarBot. Type anything to get started.
-
Type some text and send it to the bot. The bot responds with a login prompt.
-
Select the Login button. The emulator prompts you to confirm the URL that starts with
oauthlink://https://token.botframeworkcom
. Select Confirm to continue. -
In the pop-up window, login with your Microsoft 365 account. Review the requested permissions and accept.
-
Once authentication and consent are complete, the pop-up window provides a validation code. Copy the code and close the window.
-
Enter the validation code in the chat window to complete the login.
-
Try the options presented by the bot after login.
This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.
THIS CODE IS PROVIDED AS IS WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.