尝试使用Microsoft翻译器时密钥无效

时间:2019-09-30 21:06:21

标签: azure

我收到一个

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

,带有以下代码段。我已验证密钥ID是在我的仪表板中找到的密钥,并尝试了两个端点,但均未成功。

    private val subscriptionKey = System.getenv("TRANSLATOR_TEXT_SUBSCRIPTION_KEY")
    private val endpoint = 
            "https://api.cognitive.microsofttranslator.com/detect?api-version=3.0"
//            "https://westus.api.cognitive.microsoft.com/"
    var url = "$endpoint/translate?api-version=3.0&to=de,it"

    // This function performs a POST request.
    @Throws(IOException::class)
    fun post(): String {
        val client = OkHttpClient()
        val mediaType = MediaType.parse("application/json")
        val body = RequestBody.create(mediaType,
                "[{\n\t\"Text\": \"Welcome to Microsoft Translator. Guess how many languages I speak!\"\n}]")
        println("Making request with to $endpoint with key $subscriptionKey")
        val request = Request.Builder()
                .url(url).post(body)
                .addHeader("Ocp-Apim-Subscription-Key", subscriptionKey)
                .addHeader("Content-type", "application/json").build()
        val response = client.newCall(request).execute()
        return response.body().string()
    }

0 个答案:

没有答案