Android 无法通过在Kotlin中创建截击Post请求来获取Spotify访问令牌
我试图通过在Kotlin中创建POST请求来获取Spotify访问令牌,但当我尝试执行该请求时,得到400错误响应代码:Android 无法通过在Kotlin中创建截击Post请求来获取Spotify访问令牌,android,http,kotlin,post,android-volley,Android,Http,Kotlin,Post,Android Volley,我试图通过在Kotlin中创建POST请求来获取Spotify访问令牌,但当我尝试执行该请求时,得到400错误响应代码: // Obtain an access token to get authorized for the Spotify Web API val APIRequestURL = "https://accounts.spotify.com/api/token" val queue = Volley.newReq
// Obtain an access token to get authorized for the Spotify Web API
val APIRequestURL = "https://accounts.spotify.com/api/token"
val queue = Volley.newRequestQueue(requireActivity())
val postRequest = object : StringRequest(
Method.POST, APIRequestURL,
Response.Listener { response ->
iVolley!!.onResponse(response.toString())
Log.d("Access token: ", response.toString())
}, Response.ErrorListener { error -> iVolley!!.onResponse((error.message!!)) }) {
override fun getBodyContentType(): String {
return "application/x-www-form-urlencoded; charset=UTF-8"
}
@Throws(AuthFailureError::class)
override fun getHeaders(): Map<String, String> {
val credentials = "$SPOTIFY_CLIENT_ID:$SPOTIFY_CLIENT_SECRET"
val base64EncodedCredentials: String =
Base64.encodeToString(credentials.toByteArray(), Base64.NO_WRAP)
val headers: MutableMap<String, String> = HashMap()
headers["Authorization"] = "Basic $base64EncodedCredentials" // Header authorization parameter
//headers["-H Authorization"] = "Basic $base64EncodedCredentials" // Header authorization parameter
//headers["Content-Type"] = "application/json; charset=UTF-8"
return headers
}
override fun getParams(): MutableMap<String, String> {
val params = HashMap<String, String>()
params["grant-type"] = "client_credentials" // Request body parameter
//params["-d grant-type"] = "client_credentials"
return params
}
}
queue?.add(postRequest)
<> P>任何解决这个问题的帮助都被理解为身体参数->代码> GrANTHYTYPE 似乎中间有一个下划线而不是短线!“-”啊,那是我愚蠢的错误!谢谢,现在可以用了
curl -X "POST" -H "Authorization: Basic ZjM4ZjAw...WY0MzE=" -d grant_type=client_credentials https://accounts.spotify.com/api/token