<?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 Power bi rest API and authentication without RedirectURL (Powershell) in Developer</title>
    <link>https://community.fabric.microsoft.com/t5/Developer/Power-bi-rest-API-and-authentication-without-RedirectURL/m-p/631833#M18637</link>
    <description>&lt;P&gt;Hello everyone,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a problem that i can't wrap my head around.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a powershell script where im calling power bi rest api for exemple to publish reports from local .PBIX etc. All works fine when i authenticate the calls with &lt;SPAN&gt;oauth2/authorize with Redirect URL.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;But my powershell script is going to execute inside other scripts in our release, so i cannot get a GUI to login when this is in production. Therefore im going with the Client_ID &amp;amp; Client_Key in Client_Credentials instead.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Here is my authentication code now:&lt;/SPAN&gt;&lt;/P&gt;&lt;PRE&gt;function getToken{
    if (-not (Get-Module AzureRm.Profile)){
        Import-Module AzureRM.Profile
    }

    Add-Type -Path "C:\Projects\Publish\packages\Microsoft.IdentityModel.Clients.ActiveDirectory.3.19.2\lib\net45\Microsoft.IdentityModel.Clients.ActiveDirectory.Platform.dll"
    $AppURI = "https://analysis.windows.net/powerbi/api"

    $Auth = "https://login.microsoftonline.com/$Tenant/oauth2/token"
    $clientCred = [Microsoft.IdentityModel.Clients.ActiveDirectory.ClientCredential]::new($ApplicationID, "$ClientSecret") 
    
    $AuthContext = New-Object "Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext" -ArgumentList $auth
    $AuthResult = $authContext.AcquireToken($AppURI, $ClientCred)

    return $AuthResult

}&lt;/PRE&gt;&lt;P&gt;&lt;STRONG&gt;I get the Bearer token but i get a 403 (Forbidden) error when i call the API.&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What in the world am i not thinking of? permissions for the application?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Have a nice day everyone.&lt;/P&gt;</description>
    <pubDate>Tue, 26 Feb 2019 09:07:15 GMT</pubDate>
    <dc:creator>Bbooogie</dc:creator>
    <dc:date>2019-02-26T09:07:15Z</dc:date>
    <item>
      <title>Power bi rest API and authentication without RedirectURL (Powershell)</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/Power-bi-rest-API-and-authentication-without-RedirectURL/m-p/631833#M18637</link>
      <description>&lt;P&gt;Hello everyone,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a problem that i can't wrap my head around.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a powershell script where im calling power bi rest api for exemple to publish reports from local .PBIX etc. All works fine when i authenticate the calls with &lt;SPAN&gt;oauth2/authorize with Redirect URL.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;But my powershell script is going to execute inside other scripts in our release, so i cannot get a GUI to login when this is in production. Therefore im going with the Client_ID &amp;amp; Client_Key in Client_Credentials instead.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Here is my authentication code now:&lt;/SPAN&gt;&lt;/P&gt;&lt;PRE&gt;function getToken{
    if (-not (Get-Module AzureRm.Profile)){
        Import-Module AzureRM.Profile
    }

    Add-Type -Path "C:\Projects\Publish\packages\Microsoft.IdentityModel.Clients.ActiveDirectory.3.19.2\lib\net45\Microsoft.IdentityModel.Clients.ActiveDirectory.Platform.dll"
    $AppURI = "https://analysis.windows.net/powerbi/api"

    $Auth = "https://login.microsoftonline.com/$Tenant/oauth2/token"
    $clientCred = [Microsoft.IdentityModel.Clients.ActiveDirectory.ClientCredential]::new($ApplicationID, "$ClientSecret") 
    
    $AuthContext = New-Object "Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext" -ArgumentList $auth
    $AuthResult = $authContext.AcquireToken($AppURI, $ClientCred)

    return $AuthResult

}&lt;/PRE&gt;&lt;P&gt;&lt;STRONG&gt;I get the Bearer token but i get a 403 (Forbidden) error when i call the API.&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What in the world am i not thinking of? permissions for the application?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Have a nice day everyone.&lt;/P&gt;</description>
      <pubDate>Tue, 26 Feb 2019 09:07:15 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/Power-bi-rest-API-and-authentication-without-RedirectURL/m-p/631833#M18637</guid>
      <dc:creator>Bbooogie</dc:creator>
      <dc:date>2019-02-26T09:07:15Z</dc:date>
    </item>
  </channel>
</rss>

