首页 > 解决方案 > 华为移动服务(HMS)异常:云端使用ML kit Real-Time Translation时Token无效或过期?

问题描述

异常:令牌无效或过期。

这是什么错误?我已经设置了 api 密钥。这个令牌代表什么?

val setting: MLRemoteTranslateSetting =
                MLRemoteTranslateSetting.Factory()
                    .setSourceLangCode("en")
                    .setTargetLangCode("hu")
                    .create()
            val mlRemoteTranslator: MLRemoteTranslator =
                MLTranslatorFactory.getInstance().getRemoteTranslator(setting)

            try {
                val result = MLTranslateLanguage.syncGetCloudAllLanguages()
            } catch (e: MLException) {
                Log.e("ML exception", e.message.toString())
            }

            var output = ""
            try {
                output = mlRemoteTranslator.syncTranslate("SOURCE")
            } catch (e: MLException) {
                Log.e("ML exception", e.message.toString())
            }
            mlRemoteTranslator.stop()

我在这里得到这个错误:val result = MLTranslateLanguage.syncGetCloudAllLanguages() 在这里: output = mlRemoteTranslator.syncTranslate("SOURCE")

我这样设置api密钥:MLApplication.getInstance().apiKey = Utils.apiKey

object Utils {

private const val TAG = "Utils"

@JvmStatic
val apiKey: String?
    get() {
        // get apiKey from AppGallery Connect
        val apiKey = "myApiKey"

        // need encodeURI the apiKey
        return try {
            URLEncoder.encode(apiKey, "utf-8")
        } catch (e: UnsupportedEncodingException) {
            Log.e(TAG, "encode apikey error")
            null
        }
    }}

标签: androidhuawei-mobile-serviceshuawei-ml-kit

解决方案


推荐阅读