Translation API Doesn't work in swift.

태민 윤 101 Reputation points
2021-02-21T14:54:09.653+00:00

Hi.

To use translator API, I tested sample code offered from MS. (https://github.com/MicrosoftTranslator/Text-Translation-API-App-V3-IOS)
But after build the project, I got just unwrapping error below.

"Fatal error: TranslatorV3Sample[3753:856508] Fatal error: Unexpectedly found nil while unwrapping an Optional value: file TranslatorV3Sample/Dictionary.swift"

I thought there is a problem on parsing data. So I tried to take a look at a data generated in parseJson function, to add simple code "print(String(data: responseData!, encoding: .utf8)!)".

But I also got error below.

"{"error":{"code":401000,"message":"The request is not authorized because credentials are missing or invalid."}}"

I think my account has some problems(api key or something), but I can't find where it is.
Anyone knows how to fix it??

I just want to build example code correctly or test my account any way.

My subscription is free trier, and iOS version is 14.4 and I tried in Dictionary.

Thanks

Azure Translator
Azure Translator
An Azure service to easily conduct machine translation with a simple REST API call.
393 questions
0 comments No comments
{count} vote

Accepted answer
  1. 태민 윤 101 Reputation points
    2021-02-22T12:49:32.497+00:00

    I got solution!

    I can get data from api after add region information that is win azure portal, overview.

    Example code in sendRequest function is below

    { ...

    let azureKey = "*****ENTER-KEY-HERE*****"
    let azureRegion = "*****ENTER-LOCATION-HERE*****" // no space

    ...

    request.addValue(azureKey, forHTTPHeaderField: "Ocp-Apim-Subscription-Key")
    request.addValue(azureRegion, forHTTPHeaderField: "Ocp-Apim-Subscription-Region")

    ... }

    It is correct for me.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. romungi-MSFT 45,961 Reputation points Microsoft Employee
    2021-02-22T11:38:17.2+00:00

    @태민 윤 I am curious to check if you have replaced your key in the files. I think in this case it should be updated here. According to the sample:

    The app is written in Swift 4. Clone the app, get an Azure Text Translation Subsrcription, get your key, replace the text "ENTER-KEY-HERE" with your key and you are ready to run.

    This needs to be done in all the code files.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.