<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>MS 365 &#8211; My Shitty Code</title>
	<atom:link href="https://myshittycode.com/tag/ms-365/feed/" rel="self" type="application/rss+xml" />
	<link>https://myshittycode.com</link>
	<description>Embracing the Messiness in Search of Epic Solutions</description>
	<lastBuildDate>Mon, 23 Sep 2024 20:17:20 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.0</generator>

<image>
	<url>https://myshittycode.com/wp-content/uploads/2022/04/cropped-icon-32x32.png</url>
	<title>MS 365 &#8211; My Shitty Code</title>
	<link>https://myshittycode.com</link>
	<width>32</width>
	<height>32</height>
</image> 
<site xmlns="com-wordpress:feed-additions:1">205304208</site>	<item>
		<title>MSAL: Delegated Access Authentication</title>
		<link>https://myshittycode.com/2024/09/23/msal-delegated-access-authentication/</link>
					<comments>https://myshittycode.com/2024/09/23/msal-delegated-access-authentication/#respond</comments>
		
		<dc:creator><![CDATA[Shitty Author]]></dc:creator>
		<pubDate>Mon, 23 Sep 2024 20:17:14 +0000</pubDate>
				<category><![CDATA[Cloud]]></category>
		<category><![CDATA[Programming Language]]></category>
		<category><![CDATA[Azure]]></category>
		<category><![CDATA[MS 365]]></category>
		<category><![CDATA[MSAL]]></category>
		<category><![CDATA[Python]]></category>
		<guid isPermaLink="false">https://myshittycode.com/?p=2678</guid>

					<description><![CDATA[<p>The Microsoft Authentication Library (MSAL) supports various programming languages and frameworks to simplify the authentication flow against the Microsoft identity platform, which is a prerequisite to invoke APIs such as Microsoft Graph. In this example, we will write a Python script that performs delegated authentication flow using the user&#8217;s credentials. Once authenticated, the script will [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://myshittycode.com/2024/09/23/msal-delegated-access-authentication/">MSAL: Delegated Access Authentication</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<div class="wp-block-rank-math-toc-block" id="rank-math-toc"><h2>Table of Contents</h2><nav><ul><li><a href="#app-registration-and-configuration">App Registration and Configuration</a></li><li><a href="#custom-python-script">Custom Python Program</a></li></ul></nav></div>



<p class="wp-block-paragraph">The <a href="https://learn.microsoft.com/en-us/entra/msal/overview" target="_blank" rel="noopener">Microsoft Authentication Library (MSAL)</a> supports various programming languages and frameworks to simplify the authentication flow against the Microsoft identity platform, which is a prerequisite to invoke APIs such as Microsoft Graph.</p>



<p class="wp-block-paragraph">In this example, we will write a Python script that performs delegated authentication flow using the user&#8217;s credentials. Once authenticated, the script will pull the user&#8217;s OneNote data via Microsoft Graph.</p>



<p class="wp-block-paragraph"><strong>Note:</strong> While we can also authenticate via app-only access, this is generally scrutinized in larger institutions due to the blast radius. This is because the app can modify other users&#8217; content stored in Microsoft 365 products (emails, calendars, etc).</p>



<h2 class="wp-block-heading" id="app-registration-and-configuration">App Registration and Configuration</h2>



<p class="wp-block-paragraph">Even if you plan to run your program on your local machine, you must register it in the Azure Portal to generate a unique client ID to authenticate.</p>



<ul class="wp-block-list">
<li>Go to <a href="https://portal.azure.com/" target="_blank" rel="noopener">https://portal.azure.com/</a> and sign in.</li>



<li>Go to <strong>App Registrations</strong> and register your app.</li>



<li>Go to the registered app -> <strong>Manage</strong> (<em>step 1</em>) -> <strong>Authentication</strong> (<em>step </em>2). Ensure the following configurations exist:
<ul class="wp-block-list">
<li>A <strong>Mobile and desktop applications</strong> platform with the redirect URI set as <strong>http://localhost</strong> (<em>step </em>3).</li>



<li>The <strong>Access tokens (used for implicit flows)</strong> option is checked (<em>step </em>4).</li>
</ul>
</li>
</ul>



<figure class="wp-block-image size-full"><img fetchpriority="high" decoding="async" width="2114" height="2908" src="https://myshittycode.com/wp-content/uploads/2024/09/msal-authentication.png?x45560" alt="" class="wp-image-2680" srcset="https://myshittycode.com/wp-content/uploads/2024/09/msal-authentication.png 2114w, https://myshittycode.com/wp-content/uploads/2024/09/msal-authentication-218x300.png 218w, https://myshittycode.com/wp-content/uploads/2024/09/msal-authentication-744x1024.png 744w, https://myshittycode.com/wp-content/uploads/2024/09/msal-authentication-768x1056.png 768w, https://myshittycode.com/wp-content/uploads/2024/09/msal-authentication-1117x1536.png 1117w, https://myshittycode.com/wp-content/uploads/2024/09/msal-authentication-1489x2048.png 1489w" sizes="(max-width: 2114px) 100vw, 2114px" /></figure>



<ul class="wp-block-list">
<li>Go to the registered app -> <strong>Manage</strong> (<em>step 1</em>) -> <strong>API permissions</strong> (<em>step </em>2).</li>



<li>Add the permissions you need (<em>step </em>3). OneNote&#8217;s permissions are added in this example because the script will query OneNote via Microsoft Graph. </li>



<li>Before we can call the Microsoft Graph API, these permissions must have green check marks (<em>step </em>5), accomplished by clicking the <strong>Grant admin consent</strong> button (<em>step </em>4). If this button is grayed out, request your institution&#8217;s Azure administrators to perform this step on your behalf.</li>
</ul>



<figure class="wp-block-image size-full"><img decoding="async" width="2110" height="1484" src="https://myshittycode.com/wp-content/uploads/2024/09/msal-permissions.png?x45560" alt="" class="wp-image-2681" srcset="https://myshittycode.com/wp-content/uploads/2024/09/msal-permissions.png 2110w, https://myshittycode.com/wp-content/uploads/2024/09/msal-permissions-300x211.png 300w, https://myshittycode.com/wp-content/uploads/2024/09/msal-permissions-1024x720.png 1024w, https://myshittycode.com/wp-content/uploads/2024/09/msal-permissions-768x540.png 768w, https://myshittycode.com/wp-content/uploads/2024/09/msal-permissions-1536x1080.png 1536w, https://myshittycode.com/wp-content/uploads/2024/09/msal-permissions-2048x1440.png 2048w" sizes="(max-width: 2110px) 100vw, 2110px" /></figure>



<h2 class="wp-block-heading" id="custom-python-script">Custom Python Program</h2>



<p class="wp-block-paragraph">Now that the app is registered and configured in the Azure Portal, we can work on the custom script.</p>



<p class="wp-block-paragraph">Install the msal library.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: python; title: ; notranslate">
# requirements.txt

requests
msal
</pre></div>


<p class="wp-block-paragraph">Define the needed variables. The client ID and tenant ID can be retrieved from your registered app in the Azure Portal.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: python; title: ; notranslate">
import msal
import requests

CLIENT_ID = &#039;&#x5B;YOUR_CLIENT_ID]&#039;
TENANT_ID = &#039;&#x5B;TENANT_ID]&#039;
AUTHORITY = f&#039;https://login.microsoftonline.com/{TENANT_ID}&#039;
SCOPES = &#x5B;&#039;Notes.Read&#039;, &#039;User.Read&#039;]
</pre></div>


<p class="wp-block-paragraph">A function to perform the authentication and to retrieve the access token. MSAL will seamlessly create a web server running on localhost to accept the incoming token data (hence, the need to configure the redirect URI in the Azure Portal).</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: python; title: ; notranslate">
def get_access_token():
    app = msal.PublicClientApplication(CLIENT_ID, authority=AUTHORITY)
    result = app.acquire_token_interactive(scopes=SCOPES)
    access_token = result&#x5B;&#039;access_token&#039;]

    return access_token
</pre></div>


<p class="wp-block-paragraph">A simple function to query OneNote pages using Microsoft Graph.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: python; title: ; notranslate">
def get_onenote_pages(access_token):
    url = &#039;https://graph.microsoft.com/v1.0/me/onenote/pages&#039;

    headers = {
        &#039;Authorization&#039;: f&#039;Bearer {access_token}&#039;,
        &#039;Accept&#039;: &#039;application/json&#039;
    }

    response = requests.get(url, headers=headers)
    response.raise_for_status()

    return response.json()
</pre></div>


<p class="wp-block-paragraph">Finally, run the app to test if the app registration and configuration are set up correctly in the Azure Portal.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: python; title: ; notranslate">
def run():
    access_token = get_access_token()
    print(access_token)

    onenote_pages = get_onenote_pages(access_token)
    print(onenote_pages)


if __name__ == &#039;__main__&#039;:
    run() 
</pre></div><p>The post <a rel="nofollow" href="https://myshittycode.com/2024/09/23/msal-delegated-access-authentication/">MSAL: Delegated Access Authentication</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://myshittycode.com/2024/09/23/msal-delegated-access-authentication/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">2678</post-id>	</item>
	</channel>
</rss>

<!--
Performance optimized by W3 Total Cache. Learn more: https://www.boldgrid.com/w3-total-cache/?utm_source=w3tc&utm_medium=footer_comment&utm_campaign=free_plugin

Page Caching using Disk: Enhanced 
Lazy Loading (feed)
Database Caching 62/75 queries in 0.018 seconds using Disk

Served from: myshittycode.com @ 2026-07-05 14:07:09 by W3 Total Cache
-->