{"id":188,"date":"2022-01-13T11:39:37","date_gmt":"2022-01-13T03:39:37","guid":{"rendered":"https:\/\/idez.biz\/?p=188"},"modified":"2022-01-13T11:39:38","modified_gmt":"2022-01-13T03:39:38","slug":"azure-authentication-using-python-msal","status":"publish","type":"post","link":"https:\/\/idez.biz\/index.php\/azure-authentication-using-python-msal\/","title":{"rendered":"Azure Authentication using Python MSAL"},"content":{"rendered":"\n<p class=\"has-text-align-justify\">Microsoft has released <a href=\"https:\/\/github.com\/AzureAD\/microsoft-authentication-library-for-python\">MSAL for Python<\/a>. This has helped to shield away complexities in calling the API using urllib3 libraries and meddling with the parameters. In this article, I will provide sample code on how to get the access token using the following credentials type:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>UserID and Password<\/li><li>Client ID and Client Secret<\/li><\/ul>\n\n\n\n<p class=\"has-large-font-size\">User ID and Password<\/p>\n\n\n\n<p>To use user id and password similar to the grant type &#8220;password&#8221; in graph API, use the following code:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def authenticate():\r\n\r\n  authority_host_url = 'https:\/\/login.microsoftonline.com'\r\n  tenant = '&lt;Your Tenant ID>'\r\n  authority_url = authority_host_url + '\/' + tenant\r\n  client_id = '&lt;Your Client ID>'\r\n\r\n  username = '&lt;User ID>'\r\n  password = '&lt;Password>'\r\n\r\n  resource_url = '&lt;Scope URL>'\r\n\r\n  context = msal.PublicClientApplication(client_id, authority=authority_url)\r\n  response = context.acquire_token_by_username_password(username, password, scopes=&#91;resource_url])\r\n\r\n  access_token = response&#91;'access_token']\r\n\r\n  return access_token\r\n\r\n<\/code><\/pre>\n\n\n\n<p>You will need to provide the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Tenant ID<\/li><li>Client ID, this should be the application ID that you have created<\/li><li>User ID<\/li><li>Password<\/li><li>Scope URL will typically ends with \/.default<\/li><\/ul>\n\n\n\n<p class=\"has-large-font-size\">Client ID and Client Secret<\/p>\n\n\n\n<p>To use Client ID and Client Secret similar to the grant type &#8220;client_credentials&#8221; in graph API, use the following code:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def authenticate():\n\n  authority_host_url = 'https:\/\/login.microsoftonline.com'\n  tenant = '&lt;Your Tenant ID>'\n  authority_url = authority_host_url + '\/' + tenant\n  client_id = '&lt;Your Client ID>'\n  client_secret = '&lt;Your Client Secret>'\n\n  resource_url = '&lt;Scope URL>'\n\n  context = msal.ConfidentialClientApplication(client_id, authority=authority_url, client_credential=client_secret)\n  response = context.acquire_token_silent(scopes=&#91;resource_url], account=None)\n  \n  if not response:\n    response = context.acquire_token_for_client(scopes=&#91;resource_url])\r\n\n  access_token = response&#91;'access_token']\n\n  return access_token\n\n<\/code><\/pre>\n\n\n\n<p>You will need to provide the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Tenant ID<\/li><li>Client ID, this should be the application ID that you have created<\/li><li>Client Secret<\/li><li>Scope URL will typically ends with \/.default<\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Microsoft has released MSAL for Python. This has helped to shield away complexities in calling the API using urllib3 libraries and meddling with the parameters. In this article, I will provide sample code on how to get the access token using the following credentials type: UserID and Password Client ID and Client Secret User ID &#8230; <a title=\"Azure Authentication using Python MSAL\" class=\"read-more\" href=\"https:\/\/idez.biz\/index.php\/azure-authentication-using-python-msal\/\" aria-label=\"Read more about Azure Authentication using Python MSAL\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[14,13,64,67],"tags":[47,16,40,41],"class_list":["post-188","post","type-post","status-publish","format-standard","hentry","category-authentication","category-azure","category-graph-api","category-msal","tag-authentication","tag-azure","tag-msal","tag-python"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_likes_enabled":false,"jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcj45d-32","_links":{"self":[{"href":"https:\/\/idez.biz\/index.php\/wp-json\/wp\/v2\/posts\/188","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/idez.biz\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/idez.biz\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/idez.biz\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/idez.biz\/index.php\/wp-json\/wp\/v2\/comments?post=188"}],"version-history":[{"count":1,"href":"https:\/\/idez.biz\/index.php\/wp-json\/wp\/v2\/posts\/188\/revisions"}],"predecessor-version":[{"id":189,"href":"https:\/\/idez.biz\/index.php\/wp-json\/wp\/v2\/posts\/188\/revisions\/189"}],"wp:attachment":[{"href":"https:\/\/idez.biz\/index.php\/wp-json\/wp\/v2\/media?parent=188"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/idez.biz\/index.php\/wp-json\/wp\/v2\/categories?post=188"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/idez.biz\/index.php\/wp-json\/wp\/v2\/tags?post=188"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}