DmitryKatson / Blog.BC-Scripts

2 stars 3 forks source link

Redirect URI #3

Open SUD-D365 opened 5 years ago

SUD-D365 commented 5 years ago

Hi @dkatson

I am little bit confused here with your passed parameter $clientRedirectUri in powershell script. Since, Azure not allowing to write a redirect URI in this format. BusinessCentralWebServiceClient://auth image

Can you give me some insights? Thanks

SUD-D365 commented 5 years ago

Hi @dkatson

Did you faced below error while your development?

Cannot find an overload for "AcquireTokenAsync" and the argument count: "6". image

Thanks

DmitryKatson commented 5 years ago

Hi @SUD-D365

Try this image

DmitryKatson commented 5 years ago

@SUD-D365 Did you run this? image

SUD-D365 commented 5 years ago

Hi @dkatson ,

Below is what i am trying to run. image

Function definition for New-PremiumBCCloudSandBoxWithCustomData

function New-PremiumBCCloudSandBoxWithCustomData { param ( [Parameter(Mandatory=$true)] [string] $url, [Parameter(Mandatory=$true)] [string] $appId, [Parameter(Mandatory=$true)] [string] $DAemail, [string] $DApassword, [Parameter(Mandatory=$true)] [string] $tenantdomain, [Parameter(Mandatory=$true)] [string] $sandboxName, [Parameter(Mandatory=$true)] [string] $companyName

)

$startTime=(Get-Date);
Clear-Host

# Connect to admin Center
$authHeaderDA = GetAuthHeader -DAemail $DAemail -DApassword $DApassword -tenantdomain $tenantdomain -appId $appId

Write-Host $authHeaderDA

$Elapsed = (Get-Date)-$startTime;

}

Function definition for GetAuthHeader function GetAuthHeader { [CmdletBinding()] param ( [Parameter(Mandatory=$true)] [string] $DAemail, [string] $DApassword, [Parameter(Mandatory=$true)] [string] $tenantdomain, [Parameter(Mandatory=$true)] [string] $appId )

Write-Host "Checking for AzureAD module..."
if (!$CredPrompt){$CredPrompt = 'Auto'}
$AadModule = Get-Module -Name "AzureAD" -ListAvailable
if ($AadModule -eq $null) {$AadModule = Get-Module -Name "AzureADPreview" -ListAvailable}
if ($AadModule -eq $null) {write-host "AzureAD Powershell module is not installed. The module can be installed by running 'Install-Module AzureAD' or 'Install-Module AzureADPreview' from an elevated PowerShell prompt. Stopping." -f Yellow;exit}
if ($AadModule.count -gt 1) {
    $Latest_Version = ($AadModule | select version | Sort-Object)[-1]
    $aadModule      = $AadModule | ? { $_.version -eq $Latest_Version.version }
    $adal           = Join-Path $AadModule.ModuleBase "Microsoft.IdentityModel.Clients.ActiveDirectory.dll"
    $adalforms      = Join-Path $AadModule.ModuleBase "Microsoft.IdentityModel.Clients.ActiveDirectory.Platform.dll"
    }
else {
    $adal           = Join-Path $AadModule.ModuleBase "Microsoft.IdentityModel.Clients.ActiveDirectory.dll"
    $adalforms      = Join-Path $AadModule.ModuleBase "Microsoft.IdentityModel.Clients.ActiveDirectory.Platform.dll"
    }
[System.Reflection.Assembly]::LoadFrom($adal) | Out-Null
[System.Reflection.Assembly]::LoadFrom($adalforms) | Out-Null

if ($DApassword) {

    $cred = [Microsoft.IdentityModel.Clients.ActiveDirectory.UserPasswordCredential]::new($DAemail, $DApassword)
    $ctx  = [Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext]::new("https://login.windows.net/$tenantdomain")
    $client_secret = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
    $token = [Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContextIntegratedAuthExtensions]::AcquireTokenAsync($ctx, "https://api.businesscentral.dynamics.com", $appId, $client_secret, $cred).GetAwaiter().GetResult().AccessToken

    if ($token) { Write-Host -ForegroundColor Green "Successfully connected to Cloud Business Central"}
    if (!$token) { Write-Host -ForegroundColor Red "Connection to Cloud Business Central failed"}

    return "Bearer $($token)"

} 
else {

    $authority = "https://login.windows.net"
    $resource    = "https://projectmadeira.com"    
    $clientRedirectUri = [uri]"BusinessCentralWebServiceClient//auth"     

    $authenticationContext = New-Object Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext -ArgumentList "$authority/$tenantdomain"
    $platformParameters = New-Object Microsoft.IdentityModel.Clients.ActiveDirectory.PlatformParameters -ArgumentList ([Microsoft.IdentityModel.Clients.ActiveDirectory.PromptBehavior]::Always)
    $userIdentifier = New-Object Microsoft.IdentityModel.Clients.ActiveDirectory.UserIdentifier -ArgumentList ($DAemail, [Microsoft.IdentityModel.Clients.ActiveDirectory.UserIdentifierType]::RequiredDisplayableId)
    $authenticationResult = $authenticationContext.AcquireTokenAsync($resource, $appId, $clientRedirectUri, $client_secret, $platformParameters, $userIdentifier).GetAwaiter().GetResult()

    if ($authenticationResult.AccessToken) { Write-Host -ForegroundColor Green "Successfully connected to Cloud Business Central"}
    if (!$authenticationResult.AccessToken) { Write-Host -ForegroundColor Red "Connection to Cloud Business Central failed"}

    return "$($authenticationResult.AccessTokenType) $($authenticationResult.AccessToken)" 
}    

}

Thanks

SUD-D365 commented 5 years ago

@SUD-D365 Did you run this? image

Yes i ran this.

SUD-D365 commented 5 years ago

Hi @SUD-D365

Try this image

It worked for me. Thanks

DmitryKatson commented 5 years ago

Hi @dkatson

Did you faced below error while your development?

Cannot find an overload for "AcquireTokenAsync" and the argument count: "6". image

Thanks

Check here please https://github.com/microsoftgraph/powershell-intune-samples/issues/3

SUD-D365 commented 5 years ago

Seems like i have resolved this issue. Also i got a bearer key like you mentioned in blog. How do i know that sandbox is created? Or this was only about to linking with cloud business central services.