Hello @Maharjan, Narayan ,
Yes, you can make request using service principal authentication.
Here, is the sample curl command to get token and make request with that.
TENANT_ID="YOUR_TENANT_ID"
CLIENT_ID="YOUR_CLIENT_ID"
CLIENT_SECRET="YOUR_CLIENT_SECRET"
RESOURCE="https://cognitiveservices.azure.com/.default"
# Obtain an access token
ACCESS_TOKEN=$(curl -X POST "https://login.microsoftonline.com/$TENANT_ID/oauth2/v2.0/token" \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "client_id=$CLIENT_ID" \
-d "scope=$RESOURCE" \
-d "client_secret=$CLIENT_SECRET" \
-d "grant_type=client_credentials" | jq -r .access_token)
# Use the access token in the API request
payload="{\"messages\":[{\"role\":\"system\",\"content\":[{\"type\":\"text\",\"text\":\"You are an AI assistant that helps people find information.\"}]}],\"temperature\":0.7,\"top_p\":0.95,\"max_tokens\":800}"
curl "https://jgsopenai1.openai.azure.com/openai/deployments/gpt-4o-mini/chat/completions?api-version=2025-01-01-preview" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $ACCESS_TOKEN" \
-d "$payload"
After getting token like Alban Berisha mentioned you need to add it in Authorization header field.
If you observe here you need to make request to https://login.microsoftonline.com/$TENANT_ID/oauth2/v2.0/token
but as per this documentation login.microsoftonline.com
is not allowed so only options left are
- Getting token via curl or any other language and use it in sql.
- Using the Api-key.
So, choose between any of the above option.
If above solution helped you please do accept
it and give feedback by clicking on yes
.
Thank you