locked
Microsoft Translator API work using console, not working in MVC RRS feed

  • Question

  • User1703904260 posted

    I have been able to get the Microsoft Translator API to work with creating a console project.  I could only find examples with using console projects.

    When trying to get the Translator API working within a controller I am not having any luck.  I  am using the same code.

    Do I need to add some other type of reference to get the Translator to work with in MVC?

    Controller

      public async Task<string> GetAuthenticationToken(string key)
    
      {
         string endpoint = "https://api.cognitive.microsoft.com/sts/v1.0/issueToken";
    
         using (var client = new HttpClient())
         {
            client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", "xxxxxxxxxxxxxxx501b7b1ce");
            var response = await client.PostAsync(endpoint, null);
            var token = await response.Content.ReadAsStringAsync();
            return token;
         }
      }

    Getting the below error message

    {

    "name": "Microsoft.ApplicationInsights.Dev.Request",
    "time": "2017-08-17T16:32:06.3393219Z",
    "tags": {
    "ai.internal.sdkVersion": "web:2.2.0-738",
    "ai.operation.id": "lqv0XX98HCk=",
    "ai.location.ip": "::1",
    "ai.cloud.roleInstance": "ARCxxxxx.com",
    "ai.user.id": "EAbnX",
    "ai.operation.name": "GET /"
    },
    "data": {
    "baseType": "RequestData",
    "baseData": {
    "ver": 2,
    "id": "lqv0XX98HCk=",
    "name": "GET /",
    "duration": "00:00:00.0050000",
    "success": true,
    "responseCode": "401",
    "url": "http://localhost:59176/",
    "properties": {
    "DeveloperMode": "true"
    }

    Thursday, August 17, 2017 12:55 AM

Answers

  • User1703904260 posted

    A guy from HackHand.com helped me fix the problem.

    I had to change

    TranslateAsync(productTest, getUserName).Wait;

     to

     await TranslateAsync(productTest, getUserName);

     It’s working now.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 18, 2017 4:29 PM

All replies

  • User1168443798 posted

    Hi snowballzz,

    I suggest you check below link.

    # How to use Microsoft Translator API

    https://forums.asp.net/t/2127050.aspx

    Best Regards,

    Edward

    Friday, August 18, 2017 7:41 AM
  • User1703904260 posted

    A guy from HackHand.com helped me fix the problem.

    I had to change

    TranslateAsync(productTest, getUserName).Wait;

     to

     await TranslateAsync(productTest, getUserName);

     It’s working now.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 18, 2017 4:29 PM