title: get all AAD groups in which service principal is added as member
body: <p>I have a scenario, I need to write functional tests for my API(s). API uses Azure AD authentication. There are multiple roles in system and role of a user is decided on the basis of membership of different AD groups.
  so for functional test I need different users with in different groups.</p>
  
  <p>I created a service principals with contributor rights in subscription and added them in groups and granted following Microsoft graph Application type API permissions.
   - Application.Read.All
   - Directory.Read.All</p>
  
  <p>Now I used these apis to complete my use case. </p>
  
  <p>1- <a href="https://login.microsoftonline.com/" rel="nofollow noreferrer">https://login.microsoftonline.com/</a>{tenant-Id}/oauth2/token</p>
  
  <p>to get the access token against service principal.</p>
  
  <p>2- GET <a href="https://graph.microsoft.com/v1.0/me/memberOf" rel="nofollow noreferrer">https://graph.microsoft.com/v1.0/me/memberOf</a></p>
  
  <p>to get the list of user's groups. but i got following response with authentication token of service principal.</p>
  
  <pre><code>{
      "error": {
          "code": "Request_ResourceNotFound",
          "message": "Resource 'xxxx471-bxxxa-45a2-b61b-18xxxxx42af88' does not exist or one of its queried reference-property objects are not present.",
          "innerError": {
              "request-id": "fxxxxc41-319e-xxxx-xxxx-360xxxx58077",
              "date": "2020-04-13T11:41:01"
          }
      }
  }
  </code></pre>
  
  <p>I also have tried this </p>
  
  <p>3- <a href="https://graph.microsoft.com/v1.0/users/%7BprincialId%7D/memberOf" rel="nofollow noreferrer">https://graph.microsoft.com/v1.0/users/{princialId}/memberOf</a></p>
  
  <p>and get the following response</p>
  
  <pre><code>    "error": {
          "code": "Request_ResourceNotFound",
          "message": "Resource 'xxxxx-xxxx-xxxx-b61b-18421142af88' does not exist or one of its queried reference-property objects are not present.",
          "innerError": {
              "request-id": "fxxxxc41-319e-xxxx-xxxx-360xxxx58077",
              "date": "2020-04-14T05:59:03"
          }
      }
  }
  </code></pre>
  
  <p>I have used object id of app registered in azure AD. when i searched service principal using power shell using following command I found different Object_Id than which is written on AD app on azure portal</p>
  
  <p>command : get-AzureADServicePrincipal</p>
  
  <p>with this Object_Id I was able to get service principal's groups using beta services.</p>
  
  <p><a href="https://graph.microsoft.com/beta/servicePrincipals/" rel="nofollow noreferrer">https://graph.microsoft.com/beta/servicePrincipals/</a>{object ID}/memberOf</p>
  
  <p>anybody can explain why i was not able to get the groups of service principal using v1.0 service.
  Thanks</p>