0

I'm trying to get the authentication token from Monzo as in the section 'Exchange the authorization code' in the docs. When I make the request using httpie from the terminal I have no problem, but when I make the request using Volley I get a 400 response. I've confirmed that I'm using Volley correctly using the https://postman-echo.com/post endpoint.

Does the following use of Volley look sensible?

VolleyLog.DEBUG = true;
val jsonBody = JSONObject()

jsonBody.put("grant_type", "authorization_code")
jsonBody.put("client_id", "oauth2client_somestring")
jsonBody.put("client_secret", "mnzpub.somestring/somestring")
jsonBody.put("redirect_uri", "http://www.sample.com")
jsonBody.put("code", code)

val request = object : JsonObjectRequest(
    Method.POST, "https://api.monzo.com/oauth2/token", jsonBody,
    Response.Listener<JSONObject> {
        println("Got some response")
    },
    Response.ErrorListener {
        println("That didn't work!") }) {
    override fun getHeaders(): Map<String, String> {
        val params = HashMap<String, String>()
        params["Content-Type"] = "application/x-www-form-urlencoded; charset=utf-8"
        return params
    }
}
mm_857
  • 171
  • 11

1 Answers1

0

It looks like the API doesn't support application/json, having done a bit of playing about in postman. The request should be formatted as application/x-www-form-urlencoded.

mm_857
  • 171
  • 11
  • I'm having the same issue (using axios in react). I'm not sure I understand whether you solved it though. Did you just have to replace 'application/x-www-form-urlencoded' with 'application/json'? I tried that but no luck. – linuxfever Aug 18 '19 at 17:19
  • I don't know much about axios, but for me it looked like a json-formatted form wasn't supported. So I think you need to send it as application/x-www-form-urlencoded. – mm_857 Aug 18 '19 at 21:04
  • thank you, your comment helped. Have a look here if interested: https://stackoverflow.com/questions/57548019/cant-receive-monzo-access-token-using-axios/57548516#57548516 – linuxfever Aug 18 '19 at 21:31
  • I've updated my original answer as it was ambiguously written. Glad you got it working! – mm_857 Aug 19 '19 at 07:03