Forum Discussion
Jainvat
Jan 13, 2025Copper Contributor
Authentication issue while using Client Credential through Oauth2.0
Hi Community
Hope you are doing well.
I am unable to authenticate to our registered app in azure. I am looking to test the get/users graph api using insomnia (similar tool as postman).
During Token generation we are getting 401 error. we are provide correct Client ID and Secret with right scope url.
I created the app, added the necessary permissions and the client credentials.
Do I need to add a redirect uri to the app? Does the app need to be registered account types as "accounts in any organization directory"?
I am getting a 401 unauthorized error Can you please assist what I'm what is missing here?
I will really do appreciate your help.
Thanks
Vatan
- RalfLuedersCopper Contributor
I'm using powershell to access Graph.
I assigned only application permissions to the Entra registered App (not delegated permissions!)
So when connecting to Graph, no scope is needed.
But it only worked when I encrypted the access token:You could test your App registration with powershell:
$body = @{
grant_type = "client_credentials"
scope = "https://graph.microsoft.com/.default"
client_id = $clientId
client_secret = $clientSecret
}$response = Invoke-RestMethod -Uri "https://login.microsoftonline.com/$tenantId/oauth2/v2.0/token" -Method Post -Body $body
$mgAccessToken = $response.access_token
$mgSecureAccessToken = ConvertTo-SecureString $mgaccessToken -AsPlainText -Force
$mg = Connect-MgGraph -AccessToken $mgSecureAccessToken