title: Azure get list of roles assigned to Service principal in Java
body: <ol>
  <li>I have an <strong>App1</strong> (MultiTenant) which is in <strong>HomeTenant1</strong> and has
  <strong>Clientid1</strong>.</li>
  <li>This <strong>App1</strong> is registered as Service Principal in <strong>Tenant2</strong>.</li>
  <li>This <strong>App1</strong> was then assigned few roles in <strong>Tenant2</strong> on Subscription
  level scope. Ex. say <strong>Contributor</strong> role on Subs2 of Tenant2.</li>
  <li>I want to determine through Java SDK how to get list of roles
  assigned to this SP on Tenent2.</li>
  </ol>
  <p>This is possible to do via az cli
  az role assignment list --all --assignee </p>
  <p>But we want to get this via Java SDK.
  Following is the code snipped which we tried.</p>
  <pre><code> public class AzureRoles {
       private final  static String TENANT_ID = &quot;redacted&quot;; //target tenant
       private final static String CLIENT_ID = &quot;redacted&quot;; // From apps home tenant
       private final static String SUBSCRIPTIONID = &quot;redacted&quot;; //target tenant
       private final static String CLIENT_SECRET = &quot;redacted&quot;; // From apps home tenant
      
      
       public static void main(String []args) throws Exception {
           try {
               AzureProfile profile = new AzureProfile(TENANT_ID, SUBSCRIPTIONID, AzureEnvironment.AZURE);
               ClientSecretCredential clientSecretCredential = new ClientSecretCredentialBuilder()
                       .clientId(CLIENT_ID)
                       .clientSecret(CLIENT_SECRET)
                       .tenantId(TENANT_ID)
                       .build();
               System.out.println(clientSecretCredential);
               System.out.println(profile.getSubscriptionId());
               AzureResourceManager azureResourceManager = AzureResourceManager
                       .authenticate(clientSecretCredential, profile)
                       .withSubscription(SUBSCRIPTIONID);
               System.out.println(azureResourceManager);
               RoleDefinition roleDefinition = azureResourceManager.accessManagement().roleDefinitions()
                       .getByScopeAndRoleName(&quot;subscriptions/&quot; + profile.getSubscriptionId(), &quot;Contributor&quot;);
               StringBuilder builder = new StringBuilder()
                       .append(&quot;Role Definition: &quot;).append(roleDefinition.id())
                       .append(&quot;\n\tName: &quot;).append(roleDefinition.name())
                       .append(&quot;\n\tRole Name: &quot;).append(roleDefinition.roleName())
                       .append(&quot;\n\tType: &quot;).append(roleDefinition.type())
                       .append(&quot;\n\tDescription: &quot;).append(roleDefinition.description())
                       .append(&quot;\n\tType: &quot;).append(roleDefinition.type());
      
               Set&lt;Permission&gt; permissions = roleDefinition.permissions();
               builder.append(&quot;\n\tPermissions: &quot;).append(permissions.size());
               for (Permission permission : permissions) {
                   builder.append(&quot;\n\t\tPermission Actions: &quot; + permission.actions().size());
                   for (String action : permission.actions()) {
                       builder.append(&quot;\n\t\t\tName :&quot;).append(action);
                   }
                   builder.append(&quot;\n\t\tPermission Not Actions: &quot; + permission.notActions().size());
                   for (String notAction : permission.notActions()) {
                       builder.append(&quot;\n\t\t\tName :&quot;).append(notAction);
                   }
               }
      
               Set&lt;String&gt; assignableScopes = roleDefinition.assignableScopes();
               builder.append(&quot;\n\tAssignable scopes: &quot;).append(assignableScopes.size());
               for (String scope : assignableScopes) {
                   builder.append(&quot;\n\t\tAssignable Scope: &quot;)
                           .append(&quot;\n\t\t\tName :&quot;).append(scope);
               }
      
               System.out.println(builder.toString());
           } catch (Exception e) {
               System.out.println(e.getMessage());
               e.printStackTrace();
           }
      
      
       }
   }
  </code></pre>
  <p>Its throwing NPE at azureResourceManager assignment.</p>
  <p>Any ideas on how to get this done in Java SDK ?</p>
  <hr />
  <p>Update1</p>
  <p>New Code:</p>
  <pre><code>import com.azure.core.credential.TokenCredential;
  import com.azure.core.http.rest.PagedIterable;
  import com.azure.core.management.AzureEnvironment;
  import com.azure.core.management.profile.AzureProfile;
  import com.azure.identity.ClientSecretCredentialBuilder;
  import com.azure.resourcemanager.AzureResourceManager;
  import com.azure.resourcemanager.authorization.models.RoleAssignment;
  import com.azure.resourcemanager.authorization.models.RoleDefinition;
  
  
  public class AzureRoles {
  private final  static String TENANT_ID = &quot;redacted&quot;;
  private final static String HOME_TENANT_ID = &quot;redacted&quot;;
  private final static String CLIENT_ID = &quot;redacted&quot;;
  private final static String SUBSCRIPTIONID = &quot;redacted&quot;;
  private final static String CLIENT_SECRET =  &quot;redacted&quot;;
  
  
  public static void main(String []args) throws Exception {
      try {
          AzureProfile profile = new AzureProfile(TENANT_ID, SUBSCRIPTIONID, AzureEnvironment.AZURE);
          TokenCredential clientSecretCredential = new ClientSecretCredentialBuilder()
                  .clientId(CLIENT_ID)
                  .clientSecret(CLIENT_SECRET)
                  .tenantId(TENANT_ID)
                  .authorityHost(profile.getEnvironment().getActiveDirectoryEndpoint())
                  .build();
          System.out.println(clientSecretCredential);
  
          System.out.println(profile);
          AzureResourceManager azureResourceManager = AzureResourceManager
                  .authenticate(clientSecretCredential, profile)
                  .withSubscription(SUBSCRIPTIONID) ;
          System.out.println(azureResourceManager);
          PagedIterable&lt;RoleAssignment&gt; items =azureResourceManager.accessManagement().roleAssignments()
                  .listByServicePrincipal(&quot;redacted&quot;);
  
          for (RoleAssignment item:items) {
              RoleDefinition role = azureResourceManager.accessManagement().roleDefinitions().getById(item.roleDefinitionId());
              System.out.println(role.roleName());
          }
      } catch (Exception e) {
          System.out.println(e.getMessage());
          e.printStackTrace();
      }
  
  
   }
  }
  </code></pre>
  <p>Error message</p>
  <pre><code>com.azure.identity.ClientSecretCredential@5223e5ee
  com.azure.core.management.profile.AzureProfile@bef2d72
  null
  java.lang.NullPointerException
  at java.util.Objects.requireNonNull(Objects.java:203)
  at com.azure.core.http.policy.BearerTokenAuthenticationPolicy.&lt;init&gt;(BearerTokenAuthenticationPolicy.java:36)
  at com.azure.core.management.http.policy.ArmChallengeAuthenticationPolicy.&lt;init&gt;(ArmChallengeAuthenticationPolicy.java:47)
  at com.azure.resourcemanager.resources.fluentcore.policy.AuthenticationPolicy.&lt;init&gt;(AuthenticationPolicy.java:28)
  at com.azure.resourcemanager.resources.fluentcore.utils.HttpPipelineProvider.buildHttpPipeline(HttpPipelineProvider.java:74)
  at com.azure.resourcemanager.resources.fluentcore.utils.HttpPipelineProvider.buildHttpPipeline(HttpPipelineProvider.java:45)
  at com.azure.resourcemanager.AzureResourceManager.authenticate(AzureResourceManager.java:163)
  at AzureRoles.main(AzureRoles.java:32)
  </code></pre>