<?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 Re: To get Power BI API Access Token dynamically, make sure to do this in Developer</title>
    <link>https://community.fabric.microsoft.com/t5/Developer/To-get-Power-BI-API-Access-Token-dynamically-make-sure-to-do/m-p/4118626#M54976</link>
    <description>&lt;P&gt;Hi, can you please advice what access should be assigned for this to work?&lt;/P&gt;</description>
    <pubDate>Mon, 26 Aug 2024 12:47:38 GMT</pubDate>
    <dc:creator>MargaritaGG</dc:creator>
    <dc:date>2024-08-26T12:47:38Z</dc:date>
    <item>
      <title>To get Power BI API Access Token dynamically, make sure to do this</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/To-get-Power-BI-API-Access-Token-dynamically-make-sure-to-do/m-p/3606097#M46757</link>
      <description>&lt;P&gt;Im sure many people have struggled here to get this answer, so have I. I was finally able to resolve it with the help of microsoft support. I saw many posts here but none had the solution or had partial solutions, so I wanted to create a post that would lay it clear cut for anyone else looking for the same information. I watched&amp;nbsp;&lt;A href="https://www.youtube.com/watch?v=2RZkc_qrV1g&amp;amp;t=603s&amp;amp;ab_channel=BIElite" target="_self"&gt;this video&lt;/A&gt; to do this, which this person does well but there are a few things that weren't exactly clear, which I want to clarify below:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Power BI API tokens expire every hour, so it's quite annoying and not practical at all to have to copy paste the token all the time. This can be done dynamically in Power Query. For that, you'll need to create and register an app in PBI service and, which I'm not going to get into, watch the video I linked above to see how its done. The tricky part is that in the video, he uses grant_type = password. This would not work for me and was the cause of confusion.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;Some steps to consider:&amp;nbsp;&lt;/P&gt;&lt;P&gt;If your organization has &lt;STRONG&gt;MFA enabled&lt;/STRONG&gt;, then you either need to disable MFA or you cannot use your username/password to connect, so the grant_type = password cannot be used in this case.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;If you use grant_type = password and MFA is &lt;STRONG&gt;disabled&lt;/STRONG&gt;, then you need to also make sure the app &lt;STRONG&gt;has Tenant.Read.All&lt;/STRONG&gt; API permission &lt;STRONG&gt;enabled&lt;/STRONG&gt;, to be able to connect.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;If you cannot disable MFA (which was the case for me as our org wouldn't allow that ofcourse), then using the grant_type = &lt;STRONG&gt;client_credentials&lt;/STRONG&gt; is the way to go. You'll need to provide the &lt;STRONG&gt;client_id, client_secret, scope and tenant_id&lt;/STRONG&gt;. But make sure to &lt;STRONG&gt;REMOVE Tenant.Read.All&lt;/STRONG&gt; from the API permissions.&lt;BR /&gt;&lt;BR /&gt;When using service principal, you &lt;STRONG&gt;CANNOT&lt;/STRONG&gt; have Tenant.Read.All permissions allowed in the app reg. This was the problem for me. I didn't know about the MFA, so I had Tenant.Read.All permissions allowed but it still didn't work. Then once I tried with grant_type = client_credentials, it still wouldn't work because I needed to remove the Tenant.Read.All permissions for it to work.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;To summarize:&amp;nbsp;&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;When connecting with grant_type = password, you need to &lt;STRONG&gt;disable MFA&lt;/STRONG&gt; and have Tenant.Read.All permissions enabled in the API Permissions. If you cannot disable MFA, then use &lt;STRONG&gt;client_credentials&lt;/STRONG&gt; grant_type.&lt;/LI&gt;&lt;LI&gt;When connecting with grant_type = client_credentials, Tenant.Read.All &lt;STRONG&gt;must be disabled&lt;/STRONG&gt; and provide client_id, client_secret, tenant_id and scope.&amp;nbsp;&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;Hope that clears things up!&lt;BR /&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 28 Dec 2023 20:34:43 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/To-get-Power-BI-API-Access-Token-dynamically-make-sure-to-do/m-p/3606097#M46757</guid>
      <dc:creator>uahmed114pbi</dc:creator>
      <dc:date>2023-12-28T20:34:43Z</dc:date>
    </item>
    <item>
      <title>Re: To get Power BI API Access Token dynamically, make sure to do this</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/To-get-Power-BI-API-Access-Token-dynamically-make-sure-to-do/m-p/3606448#M46766</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="653138" data-lia-user-login="uahmed114pbi" class="lia-mention lia-mention-user"&gt;uahmed114pbi&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you for your sharing. I think we can try code as below to get access token in Power Query Editor. Then use this token as parameter when you get data by REST API. Access token will refresh new each time you refresh your report.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;OL type="1"&gt;
&lt;LI lang="en-US" value="1"&gt;&lt;SPAN&gt;Get Access token in Power BI Desktop:&lt;/SPAN&gt;&lt;/LI&gt;
&lt;/OL&gt;
&lt;P lang="en-US"&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;() =&amp;gt;

 

let

    body = "client_id=" &amp;amp; #"App ID"

    &amp;amp; "&amp;amp;scope=https://analysis.windows.net/powerbi/api/.default&amp;amp;client_secret=" &amp;amp; #"App Secret"

    &amp;amp; "&amp;amp;grant_type=client_credentials",

    Data= Json.Document(Web.Contents("https://login.microsoftonline.com/"&amp;amp; TenantID &amp;amp; "/oauth2/v2.0/token/",

    [Headers=[#"Content-Type"="application/x-www-form-urlencoded"], Content=Text.ToBinary(body)])),

    access_token = Data[access_token]

in

    access_token&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P lang="en-US"&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;OL type="1"&gt;
&lt;LI lang="en-US" value="2"&gt;&lt;SPAN&gt;Video: &lt;A href="https://www.youtube.com/watch?v=N8qYRSqRz84" target="_self"&gt;How to get access token and call Rest API by dynamic access token in Power BI Desktop?&lt;/A&gt;&lt;/SPAN&gt;&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;&lt;BR /&gt;Best Regards,&lt;BR /&gt;Rico Zhou&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.&lt;/P&gt;
&lt;P&gt;&lt;LI-WRAPPER&gt;&lt;/LI-WRAPPER&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 29 Dec 2023 05:45:18 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/To-get-Power-BI-API-Access-Token-dynamically-make-sure-to-do/m-p/3606448#M46766</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2023-12-29T05:45:18Z</dc:date>
    </item>
    <item>
      <title>Re: To get Power BI API Access Token dynamically, make sure to do this</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/To-get-Power-BI-API-Access-Token-dynamically-make-sure-to-do/m-p/4115736#M54945</link>
      <description>&lt;P&gt;&lt;SPAN&gt;&lt;A href="https://api.powerbi.com/v1.0/myorg/groups/{group_id}/reports/{report_id}/GenerateToken" target="_blank"&gt;https://api.powerbi.com/v1.0/myorg/groups/{group_id}/reports/{report_id}/GenerateToken&lt;/A&gt;&lt;BR /&gt;&lt;/SPAN&gt;I use the API to get the Token For power Bi Report To embed over the portal using the login API of the&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;https://login.microsoftonline.com/"&amp;amp; TenantID &amp;amp; "/oauth2/v2.0/token/&lt;/LI-CODE&gt;&lt;P&gt;&lt;BR /&gt;From This API I get the access Token, But when I call&amp;nbsp;&lt;SPAN&gt;Generate&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;Token API to get the Token I get the 401 Error I don't get the solution for this what I need to do?&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 23 Aug 2024 12:45:48 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/To-get-Power-BI-API-Access-Token-dynamically-make-sure-to-do/m-p/4115736#M54945</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2024-08-23T12:45:48Z</dc:date>
    </item>
    <item>
      <title>Re: To get Power BI API Access Token dynamically, make sure to do this</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/To-get-Power-BI-API-Access-Token-dynamically-make-sure-to-do/m-p/4118620#M54975</link>
      <description>&lt;P&gt;Hi,&lt;BR /&gt;In my case App registration was done not by me, but Tenant.ReadAll is enabled (so as I understand would need to request to disable that) -- what access should be left?&lt;BR /&gt;I tried using this in Power Query (I have MFA enabled account) and I get this (does not let me to proceed with anonymous)&lt;/P&gt;&lt;P&gt;&lt;img /&gt;&lt;/P&gt;&lt;P&gt;Can you please advise?&lt;/P&gt;</description>
      <pubDate>Mon, 26 Aug 2024 12:44:43 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/To-get-Power-BI-API-Access-Token-dynamically-make-sure-to-do/m-p/4118620#M54975</guid>
      <dc:creator>MargaritaGG</dc:creator>
      <dc:date>2024-08-26T12:44:43Z</dc:date>
    </item>
    <item>
      <title>Re: To get Power BI API Access Token dynamically, make sure to do this</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/To-get-Power-BI-API-Access-Token-dynamically-make-sure-to-do/m-p/4118626#M54976</link>
      <description>&lt;P&gt;Hi, can you please advice what access should be assigned for this to work?&lt;/P&gt;</description>
      <pubDate>Mon, 26 Aug 2024 12:47:38 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/To-get-Power-BI-API-Access-Token-dynamically-make-sure-to-do/m-p/4118626#M54976</guid>
      <dc:creator>MargaritaGG</dc:creator>
      <dc:date>2024-08-26T12:47:38Z</dc:date>
    </item>
  </channel>
</rss>

