<?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: PowerBI API:Setting access token/authentication on D365 Odata Datasrc working with scheduled ref in Developer</title>
    <link>https://community.fabric.microsoft.com/t5/Developer/PowerBI-API-Setting-access-token-authentication-on-D365-Odata/m-p/1006905#M22938</link>
    <description>&lt;P&gt;Topic bump. Still looking for answers&lt;/P&gt;</description>
    <pubDate>Fri, 03 Apr 2020 12:46:49 GMT</pubDate>
    <dc:creator>SmashDK</dc:creator>
    <dc:date>2020-04-03T12:46:49Z</dc:date>
    <item>
      <title>PowerBI API:Setting access token/authentication on D365 Odata Datasrc working with scheduled refresh</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/PowerBI-API-Setting-access-token-authentication-on-D365-Odata/m-p/626618#M18604</link>
      <description>&lt;P&gt;Hi&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have Power BI Report with a OData Dataset getting data from Dynamics365. I created in Power Bi Desktop.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Then I have a PowerBI.com workspace. Using the PowerBI.com Odata API, Azure AD API and PowerShell I am able to:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;Upload the Power BI Report to PowerBi.com. (using the powerbi.com api)&lt;/LI&gt;&lt;LI&gt;Create an AAD Access Token for Dynamics 365 (using an App Registration and a d365 user with sufficient access)&lt;/LI&gt;&lt;LI&gt;Update the dataset/datasource in the Power BI report to use the access token (using the powerbi.com api)&lt;/LI&gt;&lt;LI&gt;Refresh the dataset/datasource in the Power BI report (using the powerbi.com api)&lt;/LI&gt;&lt;LI&gt;Set up scheduled refreshs, where data is refreshed from D365 at 7am, 11am, 1pm and 15pm (also set up using PowerBi APi).&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;The refresh takes place at the requested times (or it tries to) because the refresh fails with the following error:&lt;BR /&gt;"The credentials provided for the OData source are invalid."&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="image.png" style="width: 600px;"&gt;&lt;img src="https://community.fabric.microsoft.com/t5/image/serverpage/image-id/147475iF8C02D6617A38F8E/image-size/large?v=v2&amp;amp;px=999" role="button" title="image.png" alt="image.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Since I was able to refresh earlier just after updating with the access token, I am guessing that the access token is no longer valid and PowerBi was not able to refresh it by itself.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My question is:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;How do I - using the PowerBI OData Api - update the a dataset/datasources to have valid credentials that will work when PowerBI is using refresh intervals?&lt;/LI&gt;&lt;LI&gt;I am deploying to multiple instances, so I don't want to do it manually.&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;Any help/suggestions are much appriciated.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks&lt;BR /&gt;Mads&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;---&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My code for generating the access token:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;Function Get-AADToken {
  $Username = "&amp;lt;secret-username-to-d365user-goes-here&amp;gt;"
  $Password = ConvertTo-SecureString "&amp;lt;secret-password-to-d365user-goes-here&amp;gt;" -AsPlainText -Force
  $ClientId = "&amp;lt;client-appid-to-d365-app-registration-goes-here&amp;gt;"
  $Resource = "&amp;lt;url-to-d365-instance-goes-here&amp;gt;"

  $authorityUrl = "https://login.microsoftonline.com/common/oauth2/authorize"

  ## load active directory client dll
  $typePath = $PSScriptRoot + "\Microsoft.IdentityModel.Clients.ActiveDirectory.dll"
  Add-Type -Path $typePath 

  Write-Verbose "Loaded the Microsoft.IdentityModel.Clients.ActiveDirectory.dll"

  Write-Verbose "Using authority: $authorityUrl"
  $authContext = New-Object -TypeName Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext -ArgumentList ($authorityUrl)
  $credential = New-Object -TypeName Microsoft.IdentityModel.Clients.ActiveDirectory.UserCredential -ArgumentList ($UserName, $Password)
  
  Write-Verbose "Trying to aquire token for resource: $Resource"
  $authResult = $authContext.AcquireToken($Resource, $clientId, $credential)

  Write-Verbose "Authentication Result retrieved for: $($authResult.UserInfo.DisplayableId)"
  return $authResult.AccessToken
}

$accesstoken = Get-AADToken&lt;/PRE&gt;</description>
      <pubDate>Tue, 31 Mar 2020 13:27:12 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/PowerBI-API-Setting-access-token-authentication-on-D365-Odata/m-p/626618#M18604</guid>
      <dc:creator>SmashDK</dc:creator>
      <dc:date>2020-03-31T13:27:12Z</dc:date>
    </item>
    <item>
      <title>Re: PowerBI API:Setting access token/authentication on D365 Odata Datasrc working with scheduled ref</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/PowerBI-API-Setting-access-token-authentication-on-D365-Odata/m-p/1006905#M22938</link>
      <description>&lt;P&gt;Topic bump. Still looking for answers&lt;/P&gt;</description>
      <pubDate>Fri, 03 Apr 2020 12:46:49 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/PowerBI-API-Setting-access-token-authentication-on-D365-Odata/m-p/1006905#M22938</guid>
      <dc:creator>SmashDK</dc:creator>
      <dc:date>2020-04-03T12:46:49Z</dc:date>
    </item>
  </channel>
</rss>

