Forum Discussion

Huddos's avatar
Huddos
Copper Contributor
Nov 03, 2021

Azure Function and MSAL

Hi,

 

I'm wondering why below works fine when I run within PowerShell but when I run from an Azure Function it fails with - "could not obtain authentication ticket based on provided credentials for specified" 

 

Accesskey1 and 2 are username and password

RedirectURI is https://login.microsoftonline.com/common/oauth2/nativeclient"

Scope is https://<xxx>.sharepoint.com/.default"

 

TIA

 

function Get-AADAuthToken-UserPerm([Uri] $Uri, $ClientID, $accesskey1, $accesskey2, $redirectURI, $Scope)
{
# NOTE: Create an azure app and update $clientId and $redirectUri below

#$authority = "https://login.microsoftonline.com/common"
#$authority = "https://login.microsoftonline.com/common/v2.0/.well-known/openid-configuration"
$TenantId = (Invoke-WebRequest https://login.microsoftonline.com/<domainname>/v2.0/.well-known/openid-configuration | ConvertFrom-Json).token_endpoint.Split('/')[3]
$resource = $Uri.GetLeftPart([System.UriPartial]::Authority);
$Scopes = New-Object System.Collections.Generic.List[string]
$Scopes.Add($Scope)
$pcaConfig = [Microsoft.Identity.Client.PublicClientApplicationBuilder]::Create($ClientId).WithTenantId($TenantId).WithRedirectUri($redirectURI)
$authenticationResult = $pcaConfig.Build().AcquireTokenByUsernamePassword($Scopes,$accessKey1,$accessKey2).ExecuteAsync().Result
return $authenticationResult
}

Resources