Forum Discussion
Access Admin API via a Service Principal
- 1 year ago
Hi tfanaswala
It sounds like you've set up everything correctly. Since you've got the right permissions and access, the issue is likely with the API request itself. Here are a few things you can check:1. Check the Request URL:
The Get Activities API needs specific parameters like `startDateTime` and `endDateTime`. Make sure they’re in the correct format (ISO 8601) and within a 30-day range.
Example: https://api.powerbi.com/v1.0/myorg/admin/activityevents?startDateTime=2024-03-01T00:00:00Z&endDateTime=2024-03-27T23:59:59Z2. Verify Your Token:
Double-check that you’re using a valid Bearer token with the right scopes. The token should have permissions like `Dataset.Read.All` or `Tenant.Read.All`.3. Confirm Admin API Access:
Even though the permissions look good, the service principal needs to be explicitly allowed to use the Admin API.
Go to Power BI Admin Portal → Tenant Settings → Admin API Settings and make sure the option "Allow service principals to use read-only Admin APIs" is enabled.4. Try a Simple API Call:
You could test with PowerShell to rule out other issues. Here’s an example:
powershell
Invoke-RestMethod -Uri "https://api.powerbi.com/v1.0/myorg/admin/activityevents?startDateTime=2024-03-01T00:00:00Z&endDateTime=2024-03-27T23:59:59Z" -Headers @{Authorization = "Bearer $token"}
If it returns a more detailed error, it’ll help figure out what’s wrong.5. Check Azure Logs:
If nothing works, check Azure AD logs for any failed sign-ins or authorization issues.
Go to Azure Active Directory → Monitoring → Sign-ins for more details.
If this post was helpful, please consider marking Accept as solution to assist other members in finding it more easily.If you continue to face issues, feel free to reach out to us for further assistance!
Hi tfanaswala
It sounds like you've set up everything correctly. Since you've got the right permissions and access, the issue is likely with the API request itself. Here are a few things you can check:
1. Check the Request URL:
The Get Activities API needs specific parameters like `startDateTime` and `endDateTime`. Make sure they’re in the correct format (ISO 8601) and within a 30-day range.
Example: https://api.powerbi.com/v1.0/myorg/admin/activityevents?startDateTime=2024-03-01T00:00:00Z&endDateTime=2024-03-27T23:59:59Z
2. Verify Your Token:
Double-check that you’re using a valid Bearer token with the right scopes. The token should have permissions like `Dataset.Read.All` or `Tenant.Read.All`.
3. Confirm Admin API Access:
Even though the permissions look good, the service principal needs to be explicitly allowed to use the Admin API.
Go to Power BI Admin Portal → Tenant Settings → Admin API Settings and make sure the option "Allow service principals to use read-only Admin APIs" is enabled.
4. Try a Simple API Call:
You could test with PowerShell to rule out other issues. Here’s an example:
powershell
Invoke-RestMethod -Uri "https://api.powerbi.com/v1.0/myorg/admin/activityevents?startDateTime=2024-03-01T00:00:00Z&endDateTime=2024-03-27T23:59:59Z" -Headers @{Authorization = "Bearer $token"}
If it returns a more detailed error, it’ll help figure out what’s wrong.
5. Check Azure Logs:
If nothing works, check Azure AD logs for any failed sign-ins or authorization issues.
Go to Azure Active Directory → Monitoring → Sign-ins for more details.
If this post was helpful, please consider marking Accept as solution to assist other members in finding it more easily.
If you continue to face issues, feel free to reach out to us for further assistance!
You were right that there was something wrong with the API call. I needed to wrap the dates in quotation marks, and I was going too far back in time when I was querying for events.