Skip to main content
Hello Everyone,

I'm currently in the process of integrating Salesforce with Microsoft Azure Open AI and I've run into an authentication issue when attempting to send a POST request to the Azure Open AI service. I've configured the REST API call with the Azure Open AI Endpoint, Subscription ID and API key for authentication. Keeping in mind, I've already have an Azure Open AI Account/ Subscription and I have obtained the API Key and Endpoint from the Azure Open AI. However, I'm consistently encountering the following error:

" System.HttpResponse[Status=Unauthorized, StatusCode=401] & { "statusCode": 401, "message": "Unauthorized. Access token is missing, invalid, audience is incorrect (https://cognitiveservices.azure.com), or have expired." }"

For reference, here are the details of how I've configured the authentication:

Azure Open AI Endpoint: My endpoint is in the following format: "https://{your-resource-name}.openai.azure.com/openai/deployments/{deployment-id}/chat/completions?api-version={api-version}"

: In my code, I've set it as follows:

request.setEndpoint(' https://{your-resource-name}.openai.azure.com/openai/deployments/{deployment-id}/chat/completions?api-version={api-version}');

2. Azure Open AI API Key: I've included the API Key in the request header like this: request.setHeader('Authorization', 'Bearer ' + apikey);

3. Content-Type: I've also set the Content-Type in the request header: request.setHeader('Content-Type', 'application/json');

Here is the overall snippet of the code if that helps:

public static string AI(String check){

Http http = new Http();

HttpRequest request = new HttpRequest();

request.setEndpoint(' https://{your-resource-name}.openai.azure.com/openai/deployments/{deployment-id}/chat/completions?api-version={api-version} ');

request.setMethod('POST');

request.setHeader('Content-Type', 'application/json');

request.setHeader('Authorization', 'Bearer ' + apikey);

String requestBody = '{"model":"gpt-4","messages":[{"role":"user","content":"' + check +'"}], "max_tokens":8000}'

request.setTimeout(120000);

request.setBody(requestBody);

HttpResponse response = http.send(request);

System.debug(response.getBody());

return response.getBody();

}

: I would greatly appreciate any insights or guidance on how to resolve this authentication issue and establish a successful connection between Salesforce and Microsoft Azure Open AI.
1 answer
0/9000