Set up non-interactive client login for Sitecore XM Cloud
Sitecore CLI non-interactive login for the XM world is quite a well known topic since the release of Sitecore 10. On the other hand, for Sitecore XM Cloud it is still a new terrain and there aren't (m)any basic blog posts on first-hand experience setting up the non-interactive client. So, here is that basic toddler-level blog post.
Also, while the Sitecore XM Cloud documentation has good information regarding both interactive and non-interactive login use-cases, this blog post provides a ready-to-use script for non-interactive login + screen shots to setup the Sitecore XM Cloud organization client id and client secret. I've also added a couple of issues I faced while setting up non-interactive login for my Sitecore XM Cloud environment.
As in general terms, interactive login is useful when there is a user who can respond to authentication server login prompt. On the other hand, non-interactive login is useful in case of unattended (scheduled) tasks. Non-interactive login needs a one-time setup so that there is a hand-shake between the client and the Sitecore environment to honor further requests from the client.
Sitecore XMC documentation screen shot regarding scope of the automation clients:
In my case, I wanted to access the XMC deploy environment variables so, organization automation client seems the correct choice.
1. Create Organization automation client to generate the client credentials:
3. Next, execute the PowerShell script that sets up the CLI pre-requisites and does all necessary basic setup by creating the sitecore.json file as well as other necessary folders in one-shot:
#prerequisite: .net SDK - https://dotnet.microsoft.com/en-us/download | |
[CmdletBinding()] | |
Param | |
( | |
[string] $orgClientId="org client id", | |
[string] $orgClientSecret="org client secret", | |
[string] $envId="env id" | |
) | |
function SetupCLI() | |
{ | |
param( | |
$orgClientId, | |
$orgClientSecret, | |
$envId | |
) | |
dotnet new tool-manifest --force | |
dotnet tool install Sitecore.CLI --add-source https://sitecore.myget.org/F/sc-packages/api/v3/index.json | |
dotnet tool restore | |
Dotnet sitecore init | |
dotnet sitecore plugin add -n Sitecore.DevEx.Extensibility.XMCloud | |
dotnet sitecore cloud login --authority https://auth.sitecorecloud.io/ --audience https://api.sitecorecloud.io --allow-write true --client-credentials true --client-id $orgClientId --client-secret $orgClientSecret | |
dotnet sitecore cloud environment connect --environment-id $envId --allow-write true | |
#dotnet sitecore environment set-default -n xmcloud | |
#dotnet sitecore env current | |
} | |
SetupCLI -orgClientId $orgClientId -orgClientSecret $orgClientSecret -envId $envId | |
#dotnet sitecore ser pull | |
$envVarList=dotnet sitecore cloud environment variable list --environment-id $envId --json | |
Write-Host($envVarList) |
6. On the other hand, when i created an environment automation client instead of organization automation client and tried using those client credentials, although the credentials were created for the correct environment, I received the following CLI error since I was trying to access deployment environment variables:
7. If you pass wrong client credentials, you will get a generic unexpected error in the command line:
#prerequisite: .net SDK - https://dotnet.microsoft.com/en-us/download | |
function SetupCLI() | |
{ | |
dotnet new tool-manifest --force | |
dotnet tool install Sitecore.CLI --add-source https://sitecore.myget.org/F/sc-packages/api/v3/index.json | |
dotnet tool restore | |
Dotnet sitecore init | |
dotnet sitecore plugin add -n Sitecore.DevEx.Extensibility.Serialization | |
#add more plugins here | |
dotnet sitecore login #add params for non-interactive login | |
} | |
SetupCLI | |
Write-Host("Done") |
Comments
Post a Comment