<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic [Python] Get AccessToken POST Request + Multi-factor Auth in Developer</title>
    <link>https://community.fabric.microsoft.com/t5/Developer/Python-Get-AccessToken-POST-Request-Multi-factor-Auth/m-p/585202#M17911</link>
    <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm trying to get an AccessToken from &lt;A href="https://login.microsoftonline.com/common/oauth2/token" target="_self"&gt;https://login.microsoftonline.com/common/oauth2/token&lt;/A&gt;&amp;nbsp;but I have two problems&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;My user for security reason have MFA enabled, and cannot be disabled.&lt;/LI&gt;&lt;LI&gt;The code I have found doesn't work only using clientID and ClientSecret (&lt;STRONG&gt;&lt;A href="https://azure.microsoft.com/en-us/resources/samples/data-lake-analytics-python-auth-options/" target="_self"&gt;Source&lt;/A&gt;&lt;/STRONG&gt;)&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;The code below generates an access code, but the POST request retrieves 401 status code.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I can disable MFA and enter User and Password and would generate a valid access token, but that generates a massive security problem, besides implies that I would need to have a "permanent" account that would only have access to PBI for doing refreshes - basically a flaw into the system of APIs.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;import adal
import requests
from msrestazure.azure_active_directory import AADTokenCredentials

authority_host_uri = 'https://login.microsoftonline.com'
tenant = 'tenant'
authority_uri = authority_host_uri + '/' + tenant
resource_uri = 'https://analysis.windows.net/powerbi/api'
client_id = 'clientid'
client_secret = 'clientsecret'
context = adal.AuthenticationContext(authority_uri, api_version=None)
mgmt_token = context.acquire_token_with_client_credentials(resource_uri, client_id, client_secret)
credentials = AADTokenCredentials(mgmt_token, client_id)


headers = {'Authorization': 'Bearer ' + mgmt_token['accessToken'], 'Content-Type': 'application/json'}
url = 'https://api.powerbi.com/v1.0/myorg/groups/me/datasets/datasetid'

r= requests.post(url, headers=headers)
print(r)&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 13 Dec 2018 17:03:14 GMT</pubDate>
    <dc:creator>Anonymous</dc:creator>
    <dc:date>2018-12-13T17:03:14Z</dc:date>
    <item>
      <title>[Python] Get AccessToken POST Request + Multi-factor Auth</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/Python-Get-AccessToken-POST-Request-Multi-factor-Auth/m-p/585202#M17911</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm trying to get an AccessToken from &lt;A href="https://login.microsoftonline.com/common/oauth2/token" target="_self"&gt;https://login.microsoftonline.com/common/oauth2/token&lt;/A&gt;&amp;nbsp;but I have two problems&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;My user for security reason have MFA enabled, and cannot be disabled.&lt;/LI&gt;&lt;LI&gt;The code I have found doesn't work only using clientID and ClientSecret (&lt;STRONG&gt;&lt;A href="https://azure.microsoft.com/en-us/resources/samples/data-lake-analytics-python-auth-options/" target="_self"&gt;Source&lt;/A&gt;&lt;/STRONG&gt;)&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;The code below generates an access code, but the POST request retrieves 401 status code.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I can disable MFA and enter User and Password and would generate a valid access token, but that generates a massive security problem, besides implies that I would need to have a "permanent" account that would only have access to PBI for doing refreshes - basically a flaw into the system of APIs.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;import adal
import requests
from msrestazure.azure_active_directory import AADTokenCredentials

authority_host_uri = 'https://login.microsoftonline.com'
tenant = 'tenant'
authority_uri = authority_host_uri + '/' + tenant
resource_uri = 'https://analysis.windows.net/powerbi/api'
client_id = 'clientid'
client_secret = 'clientsecret'
context = adal.AuthenticationContext(authority_uri, api_version=None)
mgmt_token = context.acquire_token_with_client_credentials(resource_uri, client_id, client_secret)
credentials = AADTokenCredentials(mgmt_token, client_id)


headers = {'Authorization': 'Bearer ' + mgmt_token['accessToken'], 'Content-Type': 'application/json'}
url = 'https://api.powerbi.com/v1.0/myorg/groups/me/datasets/datasetid'

r= requests.post(url, headers=headers)
print(r)&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 13 Dec 2018 17:03:14 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/Python-Get-AccessToken-POST-Request-Multi-factor-Auth/m-p/585202#M17911</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2018-12-13T17:03:14Z</dc:date>
    </item>
    <item>
      <title>Re: [Python] Get AccessToken POST Request + Multi-factor Auth</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/Python-Get-AccessToken-POST-Request-Multi-factor-Auth/m-p/848136#M21430</link>
      <description>&lt;P&gt;Hi ,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;You don't need to disable the MFA for your account. Instead you can overcome the issue with using SPN&lt;/P&gt;&lt;P&gt;&lt;A title="" href="https://docs.microsoft.com/en-us/power-bi/developer/embed-service-principal" target="_blank" rel="nofollow noopener noreferrer"&gt;https://docs.microsoft.com/en-us/power-bi/developer/embed-service-principal&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Just upgrade your workspace to v2.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Issue resolved !&lt;/P&gt;</description>
      <pubDate>Sun, 17 Nov 2019 06:16:44 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/Python-Get-AccessToken-POST-Request-Multi-factor-Auth/m-p/848136#M21430</guid>
      <dc:creator>Jayendran</dc:creator>
      <dc:date>2019-11-17T06:16:44Z</dc:date>
    </item>
  </channel>
</rss>

