Harrison Kuria
Harrison Kuria

Reputation: 43

Unable to integrate Mpesa Api into my android app

I am trying to integrate MPESA API into my android app using the procedures given by Daraja. On launching my application, the following error is displayed in my Logcat

D/OkHttp: {
                    "requestId": "16583-1090909-1",
                    "errorCode": "400.008.02",

D/OkHttp: "errorMessage": "Invalid grant type passed" }

and after entering a Phone number and amount to be sent, the Sim Toolkit is not being brought to the front. The following error is displayed in my Logcat

D/OkHttp: {
                    "requestId":"29178-5342114-1",

D/OkHttp: "errorCode": "404.001.04", "errorMessage": "Invalid Authentication Header" }

This was my expectation: On pressing the "Pay" button, I should be prompted to the sim toolkit requesting for my Mpesa pin to continue with the payment.

I will appreciate any assistance that will be offered.

Upvotes: 1

Views: 1030

Answers (1)

fbiego
fbiego

Reputation: 418

I had the same problem. I realized I was performing the STK push before the auth token was received. You can use a global variable to check before performing STK push

var isReady = false  // global variable
//Access token Method being called.
private fun getAccessToken() {
    mApiClient!!.setGetAccessToken(true)
    mApiClient!!.mpesaService()!!.getAccessToken().enqueue(object : Callback<AccessToken> {
        override fun onResponse(call: Call<AccessToken?>, response: Response<AccessToken>) {
            if (response.isSuccessful) {
                mApiClient!!.setAuthToken(response.body()?.accessToken)
                //Timber.e("Success: ${response.body()?.accessToken}")
                isReady = true //
            }
        }
        override fun onFailure(call: Call<AccessToken?>, t: Throwable) {}
    })
}

Upvotes: 0

Related Questions