hamzah jazi
hamzah jazi

Reputation: 55

Unauthorized. Access token is missing, invalid, audience is incorrect (https://cognitiveservices.azure.com), or have expired

I have a chatbot using C# language using Bot Framework and I'm using the Luis api to recognize intent from the user input but I'm getting an error says: Unauthorized. Access token is missing, invalid, audience is incorrect (https://cognitiveservices.azure.com), or have expired

        var GetRequest = new HttpClient();
        var url = "?q=cars";
        var MSG = new HttpRequestMessage(HttpMethod.Get, url);
        MSG.Headers.Authorization = new AuthenticationHeaderValue("Ocp-Apim-Subscription-key", "");
        var GetResult = GetRequest.SendAsync(MSG);
        var res = GetResult.Result.Content.ReadAsStringAsync().Result;
        await turnContext.SendActivityAsync(res);   

In the url I'm putting the url to call the API and I'm adding the primary key to the headers.

When I test the API on postman its working perfectly and I get the response but in the code i got the error message.

Here is the response that I get when I test it in postman

"query": "cars",
"topScoringIntent": {
    "intent": "Cars",
    "score": 0.90734994
},
"entities": []

Upvotes: 0

Views: 1938

Answers (2)

Boris
Boris

Reputation: 391

This is C# example how to make API call to Whisper model

static async Task Main(string[] args)
{
    var apiKey = "<your resource api key>";
    var endpoint = "https://<your resource endpoint>.openai.azure.com";
    var deploymentId = "<your deployment name>";
    var audioFilePath = "C:\\whisper_audio.wav";

    using (var client = new HttpClient())
    using (var content = new MultipartFormDataContent())
    using (var fileStream = new FileStream(audioFilePath, FileMode.Open, FileAccess.Read))
    using (var fileContent = new StreamContent(fileStream))
    {
        fileContent.Headers.ContentType = new MediaTypeHeaderValue("multipart/form-data");
        content.Headers.Add("api-key", apiKey); // This is the right way to apply API key in your request
        content.Add(fileContent, "file", Path.GetFileName(audioFilePath));

        var response = await client.PostAsync($"{endpoint}/openai/deployments/{deploymentId}/audio/transcriptions?api-version=2024-06-01", content);

        if (response.IsSuccessStatusCode)
        {
            var result = await response.Content.ReadAsStringAsync();
            Console.WriteLine(result);
        }
        else
        {
            var result = $"Error: {response.StatusCode}, {response.ReasonPhrase}";
            Console.WriteLine(result);
        }
    }
}

Upvotes: 0

Neil
Neil

Reputation: 11889

"Ocp-Apim-Subscription-key" is not the authorisation key, it's a simple header. There should probably be a BEARER token that you get from a login sequence.

MSG.Headers.Add("Ocp-Apim-Subscription-key", "<your subscription key>");
MSG.Headers.Authorization = new AuthenticationHeaderValue("BEARER", "<your bearer token>");

Upvotes: 1

Related Questions