0

I have an app which is protected by azureAD app registration. everytime user hits an URL, say, https://myapp.com/listall in browser they are presented with azure login screen. This is working as expected.

I was wondering how can I make curl requests to these endpoints from my terminal. When I try it now I get 302 NOT FOUND, as my request is getting redirected to login screen. when I try this form postman I see browser(HTML, CSS) code for azures login screen.

I am expecting something like.

  • make a curl request to obtain a token.
  • Use this token to subsequent curl requests.

I am making this curl request to get a token

  -H "Content-Type: application/x-www-form-urlencoded" \
  --data \
  "
    grant_type=client_credentials&
    client_id=<client-id>&
    client_secret=<client-secret>&
    resource=https%3A%2F%2Fmanagement.core.windows.net%2F
  "

I get a token back and then I make request something like this.

curl -X GET "https://myapp.com/listall" -H 'Content-Type: application/json' -H "Authorization: Bearer eyJ0eXAiOiJKV1....."

am I on the right path? how can I achieve this so that I can make curl requests to apps protected by azure AD app registration ?

Ojas Kale
  • 2,067
  • 2
  • 24
  • 39

1 Answers1

0

I tried to reproduce the same in my environment and got the access token successfully.

Please note that the resource URL you are using is for classic deployed resources and https://management.core.windows.net/ corresponds to Azure Service Management API which is now changed to https://management.azure.com/

To get the access token, I made CURL request like below:

curl -X POST -H 'Content-Type: application/x-www-form-urlencoded' \
https://login.microsoftonline.com/your_tenant_id/oauth2/token\
-d 'client_id=your_app_client_id' \
-d 'grant_type=client_credentials' \
-d 'resource=https://management.azure.com/' \
-d 'client_secret=*******************'

To call the above CURL request in Postman, please follow below steps:

Run Postman -> Import -> Select Raw Text -> Copy the above CURL request and paste in raw text field -> Continue -> Import -> Send.

enter image description here

Response:

enter image description here

After getting the token, I made API call like below:

curl -X GET "Your_API_Call_http_link" -H 'Content-Type: application/json' -H "Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJS....."

enter image description here

Make sure to grant API permissions for your API before calling it in Postman. Ensure to select access tokens and id tokens check boxes for your Azure AD app.

Sridevi
  • 10,599
  • 1
  • 4
  • 17
  • Thanks for the response @SrideviMachavarapu-MT, I can issue a token like you said. But I still get a 302 redirect on my curl command. – Ojas Kale May 24 '22 at 18:41
  • Can you include error snapshot and curl command details to know where exactly you are getting error?? – Sridevi May 25 '22 at 01:12
  • I am not getting any errors per se, It's just redirecting to login page even when token is passed in header. – Ojas Kale May 25 '22 at 16:35