Unable to obtain a Spotify access token by creating a Volley Post request in Kotlin

Viewed 195

I'm trying to obtain a Spotify access token by creating a POST request in Kotlin but I'm getting a 400 error response code when I try to execute the request:

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

This is the error I'm receiving:

E/Volley: [431382] BasicNetwork.performRequest: Unexpected response code 400 for https://accounts.spotify.com/api/token D/AndroidRuntime: Shutting down VM E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.capstone, PID: 6445 java.lang.NullPointerException at com.example.capstone.code.ui.AddSongFragment$getSongInfo$postRequest$3.onErrorResponse(AddSongFragment.kt:190) at com.android.volley.Request.deliverError(Request.java:617) at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:104) at android.os.Handler.handleCallback(Handler.java:883) at android.os.Handler.dispatchMessage(Handler.java:100) at android.os.Looper.loop(Looper.java:237) at android.app.ActivityThread.main(ActivityThread.java:8167) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:496) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1100)

I think I might be doing something wrong with my body and header parameters but I can't figure out what the problem is. This is the Spotify documentation of the POST request that I need to execute in order to obtain the access token: https://developer.spotify.com/documentation/general/guides/authorization-guide/#client-credentials-flow

As you can see in the link above, there is an example of the POST request using curl:

curl -X "POST" -H "Authorization: Basic ZjM4ZjAw...WY0MzE=" -d grant_type=client_credentials https://accounts.spotify.com/api/token

Any kind of help to solve this problem is appreciated

0 Answers
Related