Tara Prasad Gurung
Tara Prasad Gurung

Reputation: 3569

How to get users list from office 365 admin?

Recently I successfully fetched the users added to https://admin.google.com/ using the google Directory API and did a server to server authentication for the task.

Now, I need same things to be done in Office 365. Where do I start it ? Like https://admin.google.com/ what does it have in office 365.

**Is it a Azure Active Directory subscription what I need **

where and How to start with office 365 API? Though I found this https://msdn.microsoft.com/en-us/office/office365/api/api-catalog looks like it is more associated with outlook but not the users from office 365 admin

Can I get something like google-python-api-client for office 365 as I am working from python. It's not officially available here https://learn.microsoft.com/en-us/azure/active-directory/develop/active-directory-authentication-libraries

Does it have server to server authentication service like for google api where we can make the authorization with p12 or json file. Why I need this is because I am working in console application

Thanks

Upvotes: 2

Views: 2691

Answers (1)

Greg Thatcher
Greg Thatcher

Reputation: 1443

Here is a console application that gets the list of users in an Office 365/Azure Active Directory:

from azure.graphrbac import GraphRbacManagementClient
from azure.graphrbac.models import UserCreateParameters, PasswordProfile
from azure.common.credentials import UserPassCredentials

# e.g. [email protected]
admin_user_name = '<admin email account>'
admin_password = '<admin password>'
# e.g. yourcompany.onmicrosoft.com
tenant_id = "<office 365 company url>"

credentials = UserPassCredentials(
            admin_user_name,      # Your user
            admin_password,          # Your password
            resource="https://graph.windows.net"
    )

graphrbac_client = GraphRbacManagementClient(
    credentials,
    tenant_id
)
users = graphrbac_client.users.list();
for user in users :
    print(user)

This script works with Office 365 accounts (which are backed by Azure AD), and also with Azure AD instances created manually within Azure.

There is a confusing array of libraries for working with Azure, but I would suggest you start here: Azure Active Directory Graph Rbac API

Regarding your question about "server to server" authentication: You could just use the (somewhat insecure) username and password as my code does above. Alternatively, you could create an "Application Principal" in Azure AD, and grant it access to your Graph API, but in my experience, this gets hard and complicated pretty fast -- I'm not sure that the Python libraries would even have all the API calls you would need.

Upvotes: 2

Related Questions