title: Automating On-Premise Tabular Model refresh with Azure DataLake Gen1 Connection
body: <p>We are using Azure Data Lake Gen1 as source in our Tabular model. We have deployed this model on on-prem server. Now, every time we have to manually refresh the credentials to process the model. We want to automate that. I have already tried following link.</p>
  
  <p><a href="https://blog.gbrueckl.at/2017/11/processing-azure-analysis-services-oauth-sources-like-azure-data-lake-store/" rel="nofollow noreferrer">https://blog.gbrueckl.at/2017/11/processing-azure-analysis-services-oauth-sources-like-azure-data-lake-store/</a></p>
  
  <p>But gives following error</p>
  
  <p><strong>credentials provided cannot be used for the DataLake source</strong></p>
  
  <p>This is the credentials object that is getting created from script:</p>
  
  <pre><code>{
      "AuthenticationKind":  "OAuth2",
      "kind":  "DataLake",
      "path":  "https://mydatalake.azuredatalakestore.net/",
      "Expires":  "Thu, 21 Mar 2020 01:37:50 GMT",
      "AccessToken":  "",
      "RefreshToken":  ""
  }
  </code></pre>
  
  <p>Code:</p>
  
  <pre><code>[string] $ClientID = ""
  [string] $AASServerName = ""
  [string] $AASDatabaseName = ""
  [string] $CredentialName =""
  [string] $Password = ""
  
  Import-Module SqlServer
  
  $authUrl = "https://login.microsoftonline.com/{tenantid}/oauth2/v2.0/token"
  $body = @{ 
      "client_id" = $ClientID;  
      "scope" = "openid offline_access";
      "grant_type" = "password";
      "username" = $CredentialName
      "password" = $Password
  }
  
  
  
  Write-Output "Getting Authentication-Token ..." 
  $adlsToken = Invoke-RestMethod -Uri $authUrl –Method POST -Body $body
  
  $dtformat = "r"
  $dateTime = Get-Date -f $dtformat 
  $newdateTime=[datetime]::ParseExact($datetime,$dtformat,$Null).AddSeconds($adlsToken.expires_in).ToString($dtformat)
  
  #modify the token so it can be used within our ADLS Datasource
  $adlsToken | Add-Member -NotePropertyName "AuthenticationKind" "OAuth2"
  $adlsToken | Add-Member -NotePropertyName "kind" "DataLake"
  $adlsToken | Add-Member -NotePropertyName "path" "https://mydatalake.azuredatalakestore.net/"
  $adlsToken | Add-Member -NotePropertyName "Expires" $newdateTime
  $adlsToken | Add-Member -NotePropertyName "AccessToken" $adlsToken.access_token 
  $adlsToken | Add-Member -NotePropertyName "RefreshToken" $adlsToken.refresh_token 
  
  $adlsToken = $adlsToken | Select-Object -Property * -ExcludeProperty  scope,expires_in,ext_expires_in,token_type, id_token,refresh_token,access_token,token_type
  
  Write-Output "Updating DataSource with latest token ..." 
  #Update the DataSource with a valid Token
  $updateDataSource = '{
  "createOrReplace": {
  "object": {
  "database": "'+$AASDatabaseName+'",
  "dataSource": "DataLake/https://mydatalake azuredatalakestore net/xyx/data"
  },
  "dataSource": {
  "type": "structured",
  "name": "DataLake/https://mydatalake azuredatalakestore net/xyx/data",
  "connectionDetails": {
  "protocol": "data-lake-store",
  "address": {
  "url": "https://mydatalake.azuredatalakestore.net"
  }
  },
  "credential": ' + (ConvertTo-Json $adlsToken) + '
  }
  }
  }'
  
   $updateDataSource
  
  Invoke-ASCmd -Server $AASServerName -Database $AASDatabaseName -Query $updateDataSource
  
  
  
  Write-Output "Start Processing &lt;dataOnly&gt; ..." 
  $processObject = '
  {
    "refresh": {
      "type": "dataOnly",
      "objects": [ {"database": "' + $AASDatabaseName + '"} ]
    }
  }'
  Invoke-ASCmd -Server $AASServerName -Database $AASDatabaseName -Query $processObject
  
  
  Write-Output "Finished!"
  </code></pre>