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:



2. Once you click create in above dialog, you are provided with 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:


PowerShell Script:
*******
*******

Now, one error you would encounter if setting up XMC CLI in a brand new folder is this:

Error:

Environment default was not defined. Use the login command to define it.


The reason is user.json doesn't have a default defined. So, specify the same and you can do that manually by copying over an existing section:


4. Also, the proof is in the pudding: the auto-generated user.json file has the client credentials:

5. Furthermore, you can now access the XMC deploy environment variable/values.


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:


{
  "Status": "Operation failed",
  "Type": "Sitecore.DevEx.Configuration.Authentication.NotAuthenticatedException",
  "Message": "You are not authorized to perform the task you are attempting.\n                               You may need to be assigned additional permissions."
}


7. If you pass wrong client credentials, you will get a generic unexpected error in the command line:

Unexpected Error...



8. Ensure to revoke the credentials if not of any further use:


Once you revoke, since the entry is wiped-off from the above list, if you now try to login with the client credentials, part of the PS script, you will get the generic "Unexpected Error..." in the command line.

Although not different from above Sitecore CLI script, since we have come this far, here is the XM counterpart' Sitecore CLI PS Script because many a times, i search for each command in order to setup the CLI environment in a given folder.

Sitecore CLI PS Script (XM Counterpart):
*******

*******

Comments