An API that connects multiple Microsoft services, enabling data access and automation across platforms
The approach is correct, but the failure indicates that the tenant doesn’t yet “know” about the application when the service principal is being created.
The error
The appId '…' of the service principal does not reference a valid application object. code:
NoBackingApplicationObject
means Microsoft Graph cannot find a backing application object for the appId supplied when calling POST /servicePrincipals.
From the documented behavior of POST /servicePrincipals, the request body must contain a valid appId that already exists as an application in the same tenant:
POST https://graph.microsoft.com/v1.0/servicePrincipals
Content-type: application/json
{
"appId": "65415bb1-9267-4313-bbf5-ae259732ee12"
}
If that appId does not resolve to an existing application, Graph returns a bad request, as in the error seen.
This typically happens when:
- The application was just created and the directory has not fully propagated the object yet, or
- The application was created in a different tenant than the one where the service principal is being created, or
- The
appIdvalue passed is not exactly the one of the created application.
Given that the scripts create the application immediately before creating the service principal, this is most likely a propagation/timing issue.
To make the process reliable:
- After creating the application, explicitly confirm that the application is visible in Microsoft Graph using its
appIdoridbefore creating the service principal, for example by callingGET /applications?$filter=appId eq '{appId}'. - If the application is not returned, implement a short retry loop with backoff (for example, retry every few seconds up to a reasonable timeout) until the application is found.
- Only when the application is successfully retrieved, call
POST /servicePrincipalswith the sameappId.
This avoids relying on immediate consistency and removes the need for an arbitrary fixed “sleep,” while handling transient directory propagation delays.
Note that in Azure AD B2C scenarios, service principals are created the same way as in standard tenants; the requirement remains that the appId must reference an existing application object in that tenant.
This behavior is not documented as a bug; it is consistent with the requirement that POST /servicePrincipals must reference an existing application and with the eventual consistency of directory operations.
References: