{"id":6063,"date":"2021-03-09T07:00:25","date_gmt":"2021-03-09T15:00:25","guid":{"rendered":"https:\/\/officedevblogs.wpengine.com\/?p=6063"},"modified":"2021-11-17T12:33:43","modified_gmt":"2021-11-17T20:33:43","slug":"microsoft-graph-mailbag-microsoft-graph-in-electron-applications-using-microsoft-graph-toolkit","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/microsoft-graph-mailbag-microsoft-graph-in-electron-applications-using-microsoft-graph-toolkit\/","title":{"rendered":"Microsoft Graph Mailbag &#8211; Microsoft Graph in Electron applications using Microsoft Graph Toolkit"},"content":{"rendered":"<p>In today\u2019s Microsoft Graph Mailbag post, we\u2019ll explain how to build a Microsoft Graph-powered Electron application. For authentication, we\u2019ll use the Electron provider, an authentication provider in the Microsoft Graph Toolkit that\u2019s built specifically for Electron.<\/p>\n<p><img decoding=\"async\" class=\" wp-image-6077 aligncenter\" src=\"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2021\/03\/image0-1024x469.jpg\" alt=\"\" width=\"375\" height=\"172\" srcset=\"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2021\/03\/image0-1024x469.jpg 1024w, https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2021\/03\/image0-300x137.jpg 300w, https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2021\/03\/image0-768x352.jpg 768w, https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2021\/03\/image0-1536x704.jpg 1536w, https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2021\/03\/image0.jpg 1676w\" sizes=\"(max-width: 375px) 100vw, 375px\" \/><\/p>\n<p>This blog post covers the following:<\/p>\n<ul>\n<li>Authenticating users in a new Electron app by using the Electron Provider<\/li>\n<li>Adding content to the app and calling Microsoft Graph APIs using the authenticated client exposed by the Microsoft Graph Toolkit.<\/li>\n<\/ul>\n<p>The Microsoft Graph Toolkit is a collection of reusable, framework-agnostic components, and authentication providers for accessing and working with Microsoft Graph. If you want to learn more about the Microsoft Graph Toolkit, see the\u00a0<a href=\"http:\/\/aka.ms\/mgtlap\">A Lap around Microsoft Graph Toolkit<\/a><u> blog series, <\/u>and see the \u00a0<a href=\"https:\/\/docs.microsoft.com\/en-us\/graph\/toolkit\/overview\">Microsoft Graph Toolkit overview<\/a>.<\/p>\n<p>Please be sure to follow this blog series using\u00a0<a href=\"https:\/\/aka.ms\/MSGraphMailbag\">https:\/\/aka.ms\/MSGraphMailbag<\/a>\u00a0or with RSS using\u00a0<a href=\"https:\/\/developer.microsoft.com\/en-us\/graph\/blogs\/feed\/?tag=MSGraphMailbag\">https:\/\/developer.microsoft.com\/en-us\/graph\/blogs\/feed\/?tag=MSGraphMailbag<\/a>.<\/p>\n<h4>Getting started<\/h4>\n<p>The Electron provider can be used to authenticate and access Microsoft Graph in an Electron application with just a few lines of code. In this post, we\u2019ll build a\u00a0 \u00a0application that allows you to view and edit your to do tasks. You will also use the Electron provider for authentication and add some other Microsoft Graph Toolkit components to make your application look better.<\/p>\n<p>There\u2019s more! This app will also have a fun feature that changes your desktop wallpaper based on your presence status fetched from Microsoft Graph, just to give you that extra push of motivation to focus when you really need to!<\/p>\n<h4>Create an Electron app and add the Electron provider<\/h4>\n<p>This section will walk through the steps to create a simple Electron application. If you are new to Electron or want to learn more about the provider\u2019s capabilities, such as caching tokens, see <a href=\"https:\/\/docs.microsoft.com\/graph\/toolkit\/get-started\/build-an-electron-app\">Electron provider<\/a>.<\/p>\n<p>First, clone the <a href=\"https:\/\/github.com\/electron\/electron-quick-start-typescript\">electron-quickstart-typescript<\/a> repository on GitHub. We will be building on top of this boilerplate code.<\/p>\n<pre class=\"\">git clone <a href=\"https:\/\/github.com\/electron\/electron-quick-start-typescript.git\">https:\/\/github.com\/electron\/electron-quick-start-typescript.git<\/a><\/pre>\n<p>Install all the necessary packages for this project.<\/p>\n<pre class=\"\">npm install<\/pre>\n<p>Install the\u00a0@microsoft\/mgt-electron-provider\u00a0and\u00a0@microsoft\/mgt-element\u00a0npm packages as well. These will allow you to provide authentication for your app using MSAL and use the Microsoft Graph Toolkit components.<\/p>\n<pre class=\"\">npm i @microsoft\/mgt-element @microsoft\/mgt-electron-provider<\/pre>\n<p>Install the &#8216;@microsoft\/mgt-components&#8217; package that contains all the Microsoft Graph-connected web components.<\/p>\n<pre class=\"\">npm i @microsoft\/mgt-components<\/pre>\n<p>Confirm that you can run the app:<\/p>\n<pre class=\"\">npm start<\/pre>\n<h4>Initializing the provider<\/h4>\n<p>To enable authentication on your app, you will need to initialize two classes: <em>ElectronAuthenticator <\/em>and <em>ElectronProvider,<\/em> in the main and renderer processes respectively. <em>ElectronAuthenticator <\/em>is responsible for setting up the configuration variables for <a href=\"https:\/\/docs.microsoft.com\/azure\/active-directory\/develop\/msal-overview\">MSAL<\/a> authentication and acquiring access tokens. Because these tokens are needed by the components in the UI to call Microsoft Graph, we have an <em>ElectronProvider <\/em>(in the renderer process) that communicates with <em>ElectronAuthenticator\u00a0<\/em>(in the main process) to request access tokens and receive information regarding signed in state.<\/p>\n<p>Initialize the\u00a0<em>ElectronAuthenticator<\/em>\u00a0in the main process and set up the configuration variables such as client ID and required scopes.<\/p>\n<p>First, open\u00a0<em>src\/main.ts<\/em>\u00a0and import\u00a0ElectronAuthenticator\u00a0and\u00a0MsalElectronConfig as follows:<\/p>\n<pre class=\"\">import {\n  ElectronAuthenticator,\n  MsalElectronConfig,\n} from '@microsoft\/mgt-electron-provider\/dist\/Authenticator';<\/pre>\n<p>Next, add these lines of code in the\u00a0<em>createWindow()<\/em>\u00a0function to initialize the ElectronAuthenticator, right after where\u00a0<em>mainWindow<\/em>\u00a0is declared.<\/p>\n<ul>\n<li>Replace\u00a0<em>&lt;your_client_id&gt;<\/em>\u00a0with the client ID from your <a href=\"https:\/\/docs.microsoft.com\/graph\/toolkit\/providers\/electron#add-new-application-registration-in-azure-active-directory-to-get-a-client-id\">app registration<\/a><\/li>\n<li><em>mainWindow <\/em>is the BrowserWindow instance that requires authentication<\/li>\n<li><em>scopes <\/em>is a list of all the scopes that are needed to render our MGT components as well as call the presence API<\/li>\n<\/ul>\n<pre class=\"\">const\u00a0config:\u00a0MsalElectronConfig\u00a0=\u00a0{\nclientId: '&lt;your_client_id&gt;',\nmainWindow: mainWindow, \/\/BrowserWindow instance that requires auth\nscopes: [\n'user.read',\n'people.read',\n'user.readbasic.all',\n'contacts.read',\n'presence.read.all',\n'presence.read',\n'user.read.all',\n'calendars.read',\n'Sites.Read.All',\n'Sites.ReadWrite.All',\n],\n};\nElectronAuthenticator.initialize(config);<\/pre>\n<p>To initialize the\u00a0<em>ElectronProvider<\/em>, add the following code to the src\/renderer.ts file. In this code snippet, we\u2019re also importing all the MGT components so we can use them in our HTML code.<\/p>\n<p><img decoding=\"async\" class=\"wp-image-6072 alignleft\" src=\"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2021\/03\/image1.jpg\" alt=\"\" width=\"567\" height=\"148\" srcset=\"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2021\/03\/image1.jpg 1000w, https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2021\/03\/image1-300x78.jpg 300w, https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2021\/03\/image1-768x200.jpg 768w\" sizes=\"(max-width: 567px) 100vw, 567px\" \/><\/p>\n<p>And that\u2019s it! Authentication, check!<\/p>\n<h3>Adding content and Microsoft Graph integrations<\/h3>\n<p>You can now use the Microsoft Graph Toolkit components in your <em>index.html<\/em> page and show the user&#8217;s To Do tasks. For this, we\u2019re going to be using three toolkit components:<\/p>\n<ul>\n<li><a href=\"https:\/\/docs.microsoft.com\/graph\/toolkit\/components\/login\">mgt-login<\/a> (Facilitates authentication with a button to log in)<\/li>\n<li><a href=\"https:\/\/docs.microsoft.com\/graph\/toolkit\/components\/person\">mgt-person<\/a> (Shows details about the logged in user)<\/li>\n<li><a href=\"https:\/\/docs.microsoft.com\/graph\/toolkit\/components\/todo\">mgt-todo<\/a> (Enables the user to add, remove, or edit tasks from Microsoft To Do)<\/li>\n<\/ul>\n<p>Replace the content of the\u00a0<em>index.html<\/em>\u00a0page with the following.<\/p>\n<pre class=\"\">&lt;!DOCTYPE\u00a0html&gt;\n&lt;html&gt;\n  &lt;head&gt;\n    &lt;meta charset=\"UTF-8\" \/&gt;\n    &lt;title&gt;My To-Do App&lt;\/title&gt;\n    &lt;script type=\"module\" src=\".\/dist\/renderer.js\"&gt;&lt;\/script&gt;\n  &lt;\/head&gt;\n  &lt;body&gt;\n    &lt;mgt-login&gt;&lt;\/mgt-login&gt;\n    &lt;mgt-person person-query=\"me\" show-presence view=\"twoLines\" &gt;&lt;\/mgt-person&gt;\n    &lt;mgt-todo group-by-day&gt;&lt;\/mgt-todo&gt;\n  &lt;\/body&gt;\n&lt;\/html&gt;<\/pre>\n<p>We are now done with the basic version of this app!<\/p>\n<p>You can run the app with this command:<\/p>\n<pre class=\"\">npm start<\/pre>\n<p>Your app should now look like:<\/p>\n<p><img decoding=\"async\" class=\"size-full wp-image-6075 aligncenter\" src=\"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2021\/03\/image2.gif\" alt=\"\" width=\"800\" height=\"450\" \/><\/p>\n<p>That\u2019s how simple it is to get started with the toolkit and Electron \ud83d\ude0a.<\/p>\n<h4>Change your wallpaper based on presence data<\/h4>\n<p>Now for the fun part! Let\u2019s enable this app to change the desktop wallpaper based on the user\u2019s presence.<\/p>\n<p>First, l install an npm <a href=\"https:\/\/www.npmjs.com\/package\/wallpaper\">package<\/a> that makes it easy to change the desktop wallpaper:<\/p>\n<pre class=\"\">npm install wallpaper<\/pre>\n<p>Now, our aim is to detect the state of the signed in user, and change the wallpaper based on their presence. This means we need to:<\/p>\n<ol>\n<li>Determine whether there is a signed in user.<\/li>\n<li>Call a function to get presence data and change the user\u2019s wallpaper.<\/li>\n<\/ol>\n<p>To determine whether a user is signed in, in the renderer process, subscribe to the <strong>onStateChanged<\/strong> event that is fired by the <strong>globalProvider <\/strong>whenever there is a change in the sign-in state.<\/p>\n<pre class=\"\">Providers.globalProvider.onStateChanged(async\u00a0(e)\u00a0=&gt;\u00a0{\n  if (Providers.globalProvider.state === ProviderState.SignedIn) {\n    checkPresence();\n  }\n});<\/pre>\n<p>Now, define your checkPresence() function.<\/p>\n<p>For this, we need to call the <a href=\"https:\/\/docs.microsoft.com\/graph\/api\/presence-get\">presence API<\/a>. One of my favorite things about the Microsoft Graph Toolkit is that it doesn\u2019t restrict you to use only the existing components, but also provides you with an authenticated client that you can use to call any Microsoft Graph API you want, with just a few lines of code.<\/p>\n<pre class=\"\">const presence = await Providers.globalProvider.graph\n \u00a0.api('\/me\/presence')\n \u00a0.get();<\/pre>\n<p>Now, we have our presence object. We recommend that you run console.log(presence) before the next step, just to know what the object looks like.<\/p>\n<p>Let\u2019s change the wallpaper in three situations. One, when the user\u2019s presence is Do Not Disturb the other when it is Offline, and the last one for all others. We will need to use three different wallpapers.<\/p>\n<p>Now, you may proceed to spend an embarrassing amount of time looking up stuff like \u201cfunny giraffe motivational wallpapers\u201d \u2026.No I didn\u2019t do that :P.<\/p>\n<p>Finally, let\u2019s complete our checkPresence() function.<\/p>\n<pre class=\"\">async\u00a0function\u00a0checkPresence()\u00a0{\n \u00a0setInterval(async\u00a0function\u00a0()\u00a0{\n \u00a0\u00a0\u00a0\/\/Calling\u00a0presence\u00a0API\u00a0to\u00a0fetch\u00a0presence\u00a0data\n \u00a0\u00a0\u00a0const\u00a0presence\u00a0=\u00a0await\u00a0Providers.globalProvider.graph\n \u00a0\u00a0\u00a0\u00a0\u00a0.api('\/me\/presence')\n \u00a0\u00a0\u00a0\u00a0\u00a0.get();\n\n\u00a0\u00a0\u00a0\u00a0if\u00a0(presence.availability\u00a0===\u00a0'DoNotDisturb')\u00a0{\n \u00a0\u00a0\u00a0\u00a0\u00a0await\u00a0wallpaper.set(\u2018path-to-wallpaper1\u2019);\n \u00a0\u00a0\u00a0}\u00a0else\u00a0{\n \u00a0\u00a0\u00a0\u00a0\u00a0if\u00a0(presence.availability\u00a0===\u00a0'Offline')\u00a0{\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0await\u00a0wallpaper.set(\u2018path-to-wallpaper2\u2019);\n \u00a0\u00a0\u00a0\u00a0\u00a0}\u00a0else\u00a0{\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0await\u00a0wallpaper.set(path.join(\u2018path-to-wallpaper3\u2019));\n \u00a0\u00a0\u00a0\u00a0\u00a0}\n \u00a0\u00a0\u00a0}\n \u00a0},\u00a05000);\n}<\/pre>\n<p>As you may have noticed, I have set an interval of 5 seconds which is how often the presence API will be queried.<\/p>\n<p>We\u2019re all done! Now you have an Electron application that you can sign in to and authenticate using the Electron provider, display your to-do tasks, and change your wallpaper based on your Teams status.<\/p>\n<p><img decoding=\"async\" class=\"size-full wp-image-6076 aligncenter\" src=\"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2021\/03\/image3.gif\" alt=\"\" width=\"800\" height=\"450\" \/><\/p>\n<p>The working sample is on <a href=\"https:\/\/github.com\/amrutha95\/Mgt-Electron-Provider-Demo\">GitHub<\/a> if you want to run it locally and check it out.<\/p>\n<p>That\u2019s all folks! The giraffe is right, I need to get back to work. See you next time!<\/p>\n<p>&nbsp;<\/p>\n<p>Today\u2019s post was written by Amrutha Srinivasan (Software Engineer on the Microsoft Graph Toolkit team).\u00a0\u00a0You can follow Amrutha on <u><a href=\"https:\/\/www.linkedin.com\/in\/amrutha-srinivasan\/\">LinkedIn<\/a><\/u>. Join us for our next post on March 23, 2021.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>How to build a Microsoft Graph-powered Electron application using the Electron provider from the Microsoft Graph Toolkit for authentication.<\/p>\n","protected":false},"author":69075,"featured_media":25159,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[3],"tags":[34,17],"class_list":["post-6063","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-microsoft-graph","tag-microsoft-graph-toolkit","tag-msgraphmailbag"],"acf":[],"blog_post_summary":"<p>How to build a Microsoft Graph-powered Electron application using the Electron provider from the Microsoft Graph Toolkit for authentication.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/posts\/6063","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/users\/69075"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/comments?post=6063"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/posts\/6063\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/media\/25159"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/media?parent=6063"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/categories?post=6063"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/tags?post=6063"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}