为什么我从 marvel api 收到无效的凭据响应?

问题描述 投票:0回答:0

所以我正在使用 OkHTTP 调用 marvel api。然而它在回应中说

Response: {"code":"InvalidCredentials","message":"That hash, timestamp and key combination is invalid."}

我不确定问题出在哪里,因为我将其设置为与 marvel api 文档非常相似。帮助将不胜感激 Api 应该检索角色的名称、描述和缩略图

var marUrl = ""

fun md5(input: String): String {
    return try {
        val md = MessageDigest.getInstance("MD5")
        val digest = md.digest(input.toByteArray())
        Base64.encodeToString(digest, Base64.NO_WRAP)
    } catch (e: NoSuchAlgorithmException) {
        ""
    }
}

class MainActivity : AppCompatActivity() {
    private lateinit var rvCharacters: RecyclerView
    private lateinit var charactersAdapter: marveldapter


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        rvCharacters = findViewById(R.id.Marvellay)
        rvCharacters.layoutManager = LinearLayoutManager(this)

        val characters = mutableListOf<MarvelCharacter>()
        charactersAdapter = marveldapter(characters)

        rvCharacters.adapter = charactersAdapter

        fun parseCharacterData(results: JSONArray) {
            val characters = mutableListOf<MarvelCharacter>()

            for (i in 0 until results.length()) {
                val characterJson = results.getJSONObject(i)
                val id = characterJson.getInt("id")
                val name = characterJson.getString("name")
                val description = characterJson.getString("description")
                val thumbnail = characterJson.getJSONObject("thumbnail")
                val imageUrl =
                    thumbnail.getString("path") + "." + thumbnail.getString("extension")

                val character = MarvelCharacter(id, name, description, imageUrl)
                characters.add(character)
            }

            charactersAdapter.updateData(characters)

        }

        fun getmarurl() {


            val pubkey = "public_key"
            val priKey = "Private_key"
            val timestamp = System.currentTimeMillis()
            val hash = md5("$timestamp$pubkey$priKey")

            val url = "https://gateway.marvel.com/v1/public/characters".toHttpUrlOrNull()
                ?.newBuilder()
                ?.addQueryParameter("apikey",pubkey)
                ?.addQueryParameter("ts", timestamp.toString())
                ?.addQueryParameter("hash", hash)
                ?.build()

            val request = Request.Builder()
                .url(url!!)
                .build()

            val client = OkHttpClient()


            CoroutineScope(Dispatchers.IO).launch {
                try {
                    val response = client.newCall(request).execute()
                    val responseBody = response.body?.string()
                    val jsonResponse = JSONObject(responseBody)
                    Log.d("Api_Response","Response: $responseBody")
                    val data = jsonResponse.getJSONObject("data")
                    val results = data.getJSONArray("results")
                    parseCharacterData(results)
                } catch (e: IOException) {
                    // Handle the failure and alert the user
                }
            }
        }

        getmarurl()

            }
        }

我尝试重新格式化客户端,但没有任何改变

android api kotlin okhttp
© www.soinside.com 2019 - 2024. All rights reserved.