In my last post, I reviewed how to use Azure AD for service to service authentication. But sometimes, you may want to test your API directly. So let's review how to get the token via Postman.
We are going to be using the OAuth2 endpoint, and going for a "client_credentials" grant type. So you are going to need to know a few things from your Azure portal.
We are going to be using the OAuth2 endpoint, and going for a "client_credentials" grant type. So you are going to need to know a few things from your Azure portal.
- Application Id: This is used for the Client Id.
- Application key: This is used for the Client Secret. You have to generate this from the portal, and it will only give it back to you once. If you lose it, you will have to generate a new one.
- Application ID URI: we will use this as our resource. This is the Application ID URI for the app we are going to be calling, not the one we are login on as. (See What is the Resource in Azure AD for more information)
- (optional) Tenant Id: This is used to figure out where the Token Endpoint is. The new Azure portal calls this a Directory Id.
We will be using the OAuth 2.0 Token Endpoint. Or you can figure out the endpoint your self if you know your Tenant Id, it is https://login.microsoftonline.com/{{TenantID}}/oauth2/token
Now let's open up Postman and enter all the information we just collected.
Bingo, we got a Bearer token we can use to send to our service.
Bonus content:
You can also setup Postman to store the bearer token into an environment variable, automatically every time you send this request with the following script in the "Tests" tab.
var jsonData = JSON.parse(responseBody); postman.setEnvironmentVariable("AZ_AD_Token", jsonData.access_token);