Hari Umesh
Hari Umesh

Reputation: 1

Azure AI - Getting permission denied error after granting permission to the role

I am trying to access the Azure Image analysis client with below configuration

i created the application and added the role 'Cognitive Services Custom Vision Contributor' to the application

but when i try to access the image client, i am getting PermissionDenied error

tenant_id = tent_id
client_id = app_client_id
client_secret = secret_value

credential = ClientSecretCredential(
    tenant_id=tenant_id,
    client_id=client_id,
    client_secret=client_secret
)
end_point = 'https://azureaicomputervision.cognitiveservices.azure.com/'

client = ImageAnalysisClient(
    endpoint=end_point,
    credential=credential,
)

but getting following error

azure.core.exceptions.ClientAuthenticationError: (PermissionDenied) Principal does not have access to API/Operation.
Code: PermissionDenied
Message: Principal does not have access to API/Operation.

Upvotes: 0

Views: 65

Answers (1)

Suresh Chikkam
Suresh Chikkam

Reputation: 3413

You mentioned assigning "Cognitive Services Custom Vision Contributor", but for Image Analysis, the recommended role is "Cognitive Services User"

enter image description here

  • Open Microsoft Entra ID now, click on App registrations go to API permissions click Add a permission. Select Azure AI Services and Delegated permissions. Add CognitiveServices.ComputerVision. click Grant admin consent.

Install pip install azure-ai-vision azure-identity python Library.

Code:

from azure.identity import ClientSecretCredential
from azure.ai.vision.imageanalysis import ImageAnalysisClient

# Replace with your Microsoft Entra ID details
TENANT_ID = "your-tenant-id"
CLIENT_ID = "your-client-id"
CLIENT_SECRET = "your-client-secret"
ENDPOINT = "https://your-resource-name.cognitiveservices.azure.com/"

# Authenticate using Microsoft Entra ID
credential = ClientSecretCredential(
    tenant_id=TENANT_ID,
    client_id=CLIENT_ID,
    client_secret=CLIENT_SECRET
)

# Initialize Image Analysis Client
client = ImageAnalysisClient(
    endpoint=ENDPOINT,
    credential=credential,
)

# Test authentication
token = credential.get_token("https://cognitiveservices.azure.com/.default")
print("Access Token:", token.token)  # Should print a valid token

Upvotes: 0

Related Questions