Microsoft-graph-docs: Create appRoleAssignment does not work.

Created on 26 Aug 2019  Â·  3Comments  Â·  Source: microsoftgraph/microsoft-graph-docs

This API method returns the following:
{
"error": {
"code": "Request_BadRequest",
"message": "One or more properties are invalid.",
"innerError": {
"request-id": "6ab3a024-2078-40fe-8460-50f3dcf589b8",
"date": "2019-08-26T13:39:52"
}
}
}

Here are links referencing the same issue. There doesn't seem to be anyone working on this issue. Please fix this.

https://github.com/microsoftgraph/microsoft-graph-docs/issues/4266

https://stackoverflow.com/questions/56786453/how-to-create-an-approleassignment-via-microsoft-graph

https://github.com/microsoftgraph/msgraph-beta-sdk-dotnet/issues/2


Document Details

⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

app model azurAAD

Most helpful comment

Check my response here: https://stackoverflow.com/questions/56786453/how-to-create-an-approleassignment-via-microsoft-graph

        const string ROLE_ASSIGNMENT_FORMATTER = "https://graph.microsoft.com/beta/servicePrincipals/{0}/appRoleAssignments";

        public static async Task AddApplicationUsers(string enterpriseAppId, string userId, string roleId)
        {
            HttpClient client = new HttpClient();
            string url = string.Format(ROLE_ASSIGNMENT_FORMATTER, enterpriseAppId);

            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", await GetAccessToken());

            var roleAssignment = new
            {
                appRoleId = roleId,
                principalId = userId,
                resourceId = enterpriseAppId
            };


            var content = new StringContent(Newtonsoft.Json.JsonConvert.SerializeObject(roleAssignment), Encoding.UTF8, "application/json");
            content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
            var response  = await client.PostAsync(url, content);

            if (response.IsSuccessStatusCode)
            {
                return ;
            }
            else
            {
                throw new HttpRequestException(response.ReasonPhrase);
            }
        }

All 3 comments

Check my response here: https://stackoverflow.com/questions/56786453/how-to-create-an-approleassignment-via-microsoft-graph

        const string ROLE_ASSIGNMENT_FORMATTER = "https://graph.microsoft.com/beta/servicePrincipals/{0}/appRoleAssignments";

        public static async Task AddApplicationUsers(string enterpriseAppId, string userId, string roleId)
        {
            HttpClient client = new HttpClient();
            string url = string.Format(ROLE_ASSIGNMENT_FORMATTER, enterpriseAppId);

            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", await GetAccessToken());

            var roleAssignment = new
            {
                appRoleId = roleId,
                principalId = userId,
                resourceId = enterpriseAppId
            };


            var content = new StringContent(Newtonsoft.Json.JsonConvert.SerializeObject(roleAssignment), Encoding.UTF8, "application/json");
            content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
            var response  = await client.PostAsync(url, content);

            if (response.IsSuccessStatusCode)
            {
                return ;
            }
            else
            {
                throw new HttpRequestException(response.ReasonPhrase);
            }
        }

Thanks for the response! That worked perfectly.

In the SO thread, it appears that the issue was cleared up. No change identified for the page. Closing the issue.

Was this page helpful?
0 / 5 - 0 ratings