title: How to use microsoft graph api for assigning role to the user in azure ad
body: <p>I have created below role in the <code>app registration manifest</code>:</p>
  <pre><code>&quot;appRoles&quot;: [
     {
        &quot;allowedMemberTypes&quot;: [
          &quot;User&quot;
        ],
        &quot;displayName&quot;: &quot;Student&quot;,
        &quot;id&quot;: &quot;d1c2ade8-98f8-45fd-aa4a-6d06b947c66f&quot;,
        &quot;isEnabled&quot;: true,
        &quot;description&quot;: &quot;Student&quot;,
        &quot;value&quot;: &quot;Student&quot;
      }
    ],
  </code></pre>
  <p>Now I am using <code>appRoleAssignment</code> api to assign a role to the user. I am following this <a href="https://docs.microsoft.com/en-us/graph/api/serviceprincipal-post-approleassignments?view=graph-rest-1.0&amp;tabs=http" rel="nofollow noreferrer">documentation</a>. In this page, it says that we need to use below api with the json body:</p>
  <pre><code>POST https://graph.microsoft.com/v1.0/servicePrincipals/{id}/appRoleAssignments
  Content-Type: application/json
  Content-Length: 110
  
  {
    &quot;principalId&quot;: &quot;principalId-value&quot;,
    &quot;resourceId&quot;: &quot;resourceId-value&quot;,
    &quot;appRoleId&quot;: &quot;appRoleId-value&quot;
  }
  </code></pre>
  <p>I am unable to understand what I should use in <code>principalId</code>, <code>resourceId</code> and <code>appRoleId</code>. As per that page, it says that:</p>
  <pre><code>principalId: The id of the client service principal to which you are assigning the app role.
  resourceId: The id of the resource servicePrincipal (the API) which has defined the app role (the application permission).
  appRoleId: The id of the appRole (defined on the resource service principal) to assign to the client service principal.
  </code></pre>
  <p>But what I could understand is that <code>principalId is the ID of the user I have in the active directory for which I want to assign the role.</code></p>
  <p>which in my case is the ObjectId in below photo:</p>
  <p><a href="https://i.stack.imgur.com/voqFX.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/voqFX.jpg" alt="enter image description here" /></a></p>
  <p>is this correct.?</p>
  <p><code>resourceId</code> is the tennant id and <code>appRoleId</code> is the id I used while creating the app role above which is <code>d1c2ade8-98f8-45fd-aa4a-6d06b947c66f</code></p>
  <p>Putting it all together if I make a request in python</p>
  <pre><code>token = get_token()
  headers = {'Authorization': 'Bearer ' + token, 'Content-Type': 'application/json'}
  
  user_data = {
      &quot;principalId&quot;: &quot;1bc79085-12qw-4fad-8da8-647f4b4b2927&quot;,  
      &quot;resourceId&quot;: &quot;c01b6482-3ccd-4533-8c98-a7c5e8067cc8&quot;,   
      &quot;appRoleId&quot;: &quot;d1c2ade8-98f8-45fd-aa4a-6d06b947c66f&quot;  
  }
  j_data = json.dumps(user_data)
  conn = http.client.HTTPSConnection('graph.microsoft.com')
  conn.request(&quot;POST&quot;, &quot;/v1.0/servicePrincipals/1bc79085-12qw-4fad-8da8-647f4b4b2927/appRoleAssignments&quot;, j_data, headers)
  response = conn.getresponse()
  rdata = response.read()
  </code></pre>
  <p>I am getting below response:</p>
  <pre><code>{
    &quot;error&quot;: {
      &quot;code&quot;: &quot;Request_ResourceNotFound&quot;,
      &quot;message&quot;: &quot;Resource '1bc79085-12qw-4fad-8da8-647f4b4b2927' does not exist or one of its queried reference-property objects are not present.&quot;,
      &quot;innerError&quot;: {
        &quot;date&quot;: &quot;2020-10-26T05:16:35&quot;,
        &quot;request-id&quot;: &quot;1c87a140-7bc9-499d-82dd-bc1dcb54e075&quot;,
        &quot;client-request-id&quot;: &quot;1c87a140-7bc9-499d-82dd-bc1dcb54e075&quot;
      }
    }
  }
  </code></pre>
  <p>Can anyone please help me debug this. Please help. Thanks</p>
  <p>EDIT:</p>
  <p>Error:</p>
  <pre><code>{
      &quot;error&quot;: {
          &quot;code&quot;: &quot;Request_ResourceNotFound&quot;,
          &quot;message&quot;: &quot;Resource '261eda4b-6eee-45ba-a176-259960603409' does not exist or one of its queried reference-property objects are not present.&quot;,
          &quot;innerError&quot;: {
              &quot;date&quot;: &quot;2020-10-26T07:09:38&quot;,
              &quot;request-id&quot;: &quot;8dc2ea73-63e5-45b5-8127-445df777c1e1&quot;,
              &quot;client-request-id&quot;: &quot;8dc2ea73-63e5-45b5-8127-445df777c1e1&quot;
          }
      }
  }
  </code></pre>
  <p>Json:</p>
  <pre><code>{
      &quot;principalId&quot;: &quot;f923e078-ca9d-4611-a80e-bebb712ad7d1&quot;,  
      &quot;resourceId&quot;: &quot;261eda4b-6eee-45ba-a176-259960603409&quot;,   
      &quot;appRoleId&quot;: &quot;d1c2ade8-98f8-45fd-aa4a-6d06b947c66f&quot;  
  }
  </code></pre>
  <p>Post URL: <a href="https://graph.microsoft.com/v1.0/servicePrincipals/261eda4b-6eee-45ba-a176-259960603409/appRoleAssignments" rel="nofollow noreferrer">https://graph.microsoft.com/v1.0/servicePrincipals/261eda4b-6eee-45ba-a176-259960603409/appRoleAssignments</a></p>
  <p>GET Url to get the object id: <a href="https://graph.microsoft.com/v1.0/serviceprincipals?$select=id&amp;$filter=displayName" rel="nofollow noreferrer">https://graph.microsoft.com/v1.0/serviceprincipals?$select=id&amp;$filter=displayName</a> eq '{useracces}'</p>
  <p><a href="https://i.stack.imgur.com/3TeUy.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/3TeUy.jpg" alt="enter image description here" /></a></p>