I use a windows pwsh script to receive an AccessToken. The code works without a problem on my own pc and on a virtual windows server in the cloud.
We would like to run this code on a ubuntu virtual machine since the other steps are all ready integrated in this ubuntu machine and running them on the windows server is giving some compatibility issues.
This is the code right now:
# Get JWT using certificate# PowerShell 7 required# Required modules# Install-Module -name MSAL.PS -Force -AcceptLicense# Install-Module -Name JWTDetailsImport-Module MSAL.PS$clientID = 'xxx'$tenantID = 'yyy'$ClientCertificateLocation = 'Cert:\LocalMachine\My\zzz'$ClientCertificate = Get-Item $ClientCertificateLocation$tokenScope = 'api://aaa/.default'$myAccessToken = Get-MsalToken -ClientId $clientID -TenantId $tenantID -ClientCertificate $ClientCertificate -Scope $tokenScope# Inspect and print the Access Token using JWTDetails PowerShell Module#$myAccessToken.AccessToken | Get-JWTDetails$myAccessToken.AccessToken | Set-Clipboard;$myAccessToken.AccessToken | Write-output;
I think the main issue is the difference with how the certificates are saved and used between windows and ubuntu. I have tried multiple variations of the code with certificates in different formats but have not found the right solution.
The main warning the ubuntu code gives me is that they cannot find the secret key while the secret key is in the certificate or is even given separatly.
Any ideas?I'm also open to use Python or something else to migrate to ubuntu. Tested with Python, but gave the same type of error.I did install the pwsh package for ubuntu.
This is one of the code tries on ubuntu pwsh:
# Required modules# Install-Module -name MSAL.PS -Force -AcceptLicense# Install-Module -Name JWTDetailsImport-Module MSAL.PS$clientID = 'xxx'$tenantID = 'yyy'$CertificateLocation = '/home/ubuntu/zzz_be.crt'$PrivateKeyLocation = '/home/ubuntu/private.key'$tokenScope = 'api://zzz/.default'# Load certificate and private key$Certificate = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2$Certificate.Import($CertificateLocation)$PrivateKey = Get-Content $PrivateKeyLocation -Raw# Acquire token$myAccessToken = Get-MsalToken -ClientId $clientID -TenantId $tenantID -ClientCertificate $Certificate -PrivateKey $PrivateKey -Scope $tokenScope# Check if the token is obtained successfullyif ($myAccessToken.AccessToken -ne $null) { # Inspect and print the Access Token using JWTDetails PowerShell Module $myAccessToken.AccessToken | Get-JWTDetails} else { Write-Host "Failed to obtain the access token."}