1

exception: Token is invalid or expired.

What is this error? I have already set the api key. And what does this token represent?

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()

I get this error here: val result = MLTranslateLanguage.syncGetCloudAllLanguages() and here: output = mlRemoteTranslator.syncTranslate("SOURCE")

I set the api key like this: 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
        }
    }}
zhangxaochen
  • 32,744
  • 15
  • 77
  • 108
Botond Sulyok
  • 271
  • 3
  • 14
  • The setting is correct. Could you please send your api key to hmscore@huawei.com for futher analyzation?You can also perform the configuration by referring to [Notes on Using Cloud Authentication Information](https://developer.huawei.com/consumer/en/doc/development/HMSCore-Guides-V5/sdk-data-security-0000001050040129-V5#EN-US_TOPIC_0000001050750251__section2688102310166). – zhangxaochen Jan 11 '21 at 06:22
  • Thanks. I have sent my api key. – Botond Sulyok Jan 11 '21 at 09:48
  • The length of your api key seems to be incorrect. It is recommended that you could delete the current application on the console and create a new application. Hope it helps! @Botond Sulyok – zhangxaochen Jan 12 '21 at 01:23
  • I did it, but unfortunately it doesn't help and the length of the API key is the same. @shirley – Botond Sulyok Jan 12 '21 at 20:52
  • I didn't set an access token which may be a problem. MLApplication.getInstance().setAccessToken("your access token") // but here I don't actually know what is this token for – Botond Sulyok Jan 12 '21 at 20:53
  • You can [setApiKey](https://developer.huawei.com/consumer/en/doc/development/HMSCore-References-V5/mlapplication-0000001050167420-V5#EN-US_TOPIC_0000001050167420__section165441628134218) instead of [setAccessToken](https://developer.huawei.com/consumer/en/doc/development/HMSCore-References-V5/mlapplication-0000001050167420-V5#EN-US_TOPIC_0000001050167420__section191234171515). – zhangxaochen Jan 13 '21 at 01:00

0 Answers0