You have to be authenticated to access your Azure subscription and deallocate any Azure VM.
If you want to do this programmatically, you'd need to configure the application to sign in to Entra ID non-interactively (also known as service principal authentication). To do so, you'd follow these steps:
Register the Application:
Go to the Azure portal and navigate to "Azure Active Directory" > "App registrations".
Click on "New registration" and provide a name for your application.
Choose the appropriate supported account types (e.g., single tenant or multi-tenant).
Specify the Redirect URI if your application requires it. For non-interactive authentication, this is typically not needed.
Click on "Register" to create the application.
Create Client Secret:
Once the application is registered, navigate to the "Certificates & secrets" section.
Click on "New client secret" and enter a description.
Choose the expiration duration for the secret (recommended to choose a long expiration time for service principals).
Click on "Add" to generate the client secret. Make sure to copy and securely store the secret value, as it will not be displayed again.
**Assign Required Permissions**:
Navigate to the "API permissions" section of your registered application.
Click on "Add a permission" and select the required APIs or Microsoft Graph permissions that your application needs to access.
Grant admin consent if necessary.
**Retrieve Tenant ID and Application ID**:
Note down the "Directory (tenant) ID" and the "Application (client) ID" of your registered application. You'll need these values for authentication.
**Use Client Credentials Flow**:
In your application code, use the Client Credentials OAuth 2.0 flow to authenticate using the client ID and client secret.
Make a POST request to the token endpoint (**`https://login.microsoftonline.com/{tenant_id}/oauth2/v2.0/token`**) with the following parameters:
**`client_id`**: Application (client) ID of your registered application.
**`client_secret`**: The client secret generated earlier.
**`grant_type`**: Set to "client_credentials".
**`scope`**: Set to the desired scope (e.g., **`"https://graph.microsoft.com/.default"`** for Microsoft Graph API).
Upon successful authentication, you'll receive an access token that can be used to access the requested resources on behalf of the application.
If the above response helps answer your question, remember to "Accept Answer" so that others in the community facing similar issues can easily find the solution. Your contribution is highly appreciated.
hth
Marcin