How to fix twitter response code=400, message=Bad Request?

214 Views Asked by At

I integrated the witter login in android. But on response it always returning me in the logcat:

code=400, message=Bad Request

and

"data":"{\"+clicked_branch_link\":false,\"+is_first_session\":false}",

I am to see the token, secret value in logcat if I printed it. But response returning 400 always. I used here branch IO for deep linking.

 public void attachTwitter(String token, String secret) {
        apiService.attachTwitterAccount(PreferenceHandler.readString(EditProfileActivity.this, SIGNIN_ID, ""),
                token, secret, "twitter").enqueue(new Callback<Object>() {

            @Override
            public void onResponse(Call<Object> call, Response<Object> response) {
                Log.i("accessToken", "onResponse");
                if (!response.isSuccessful()) {
                    try {
                        JSONObject object = new JSONObject(response.errorBody().string());
                        JSONObject error = object.optJSONObject("error");
                        String code = error.optString("code");
                        String description = error.optString("description");

                        if (code.equalsIgnoreCase("338")) {
                            showCustomDialog(EditProfileActivity.this, string(R.string.server_error_description_338));
                            switchTwitter.setChecked(false);
                        }

                    } catch (Exception e) {

                    }
                }
            }

            @Override
            public void onFailure(Call<Object> call, Throwable t) {
                Log.i("accessToken", "onFailure");
                switchTwitter.setChecked(false);
            }
        });
    } 

attachTwitterAccount methods code is:

@FormUrlEncoded
    fun attachTwitterAccount(@Field("id") id: String,
                             @Field("authToken") token: String,
                             @Field("authSecret") authSecret: String,
                             @Field("typeAttach") typeAttach: String): Call<Any>

Can anyone please advise how I can fix this issue?

1

There are 1 best solutions below

1
On

A Bad request means that the request that you are sending to the server is not in the way or form it is expecting. What do the docs say about that request? is it a Post? a Get?. If it is a POST then you should send a Body.

To send a body and a POST you first need to create a class for the body. In Kotlin it would be something like this:

class Body(var id: String,
                        var authToken: String,
                        var authSecret: String,
                        var accomplished: Double,
                        var typeAttach: String
                        )

Then you call the body in your request:

@POST("post value which")
fun attachTwitterAccount(@Body body:Body): Call<Any>

Finally you call it from your attach Twitter function. You create your Body instance and then pass it as argument.

If you are still getting the same error, check the Twitter docs, you might be missing something.

Its always a good idea to try that same call to the server in other enviroment (such as Postman) and see if it works there? this is a good way of determining if the problem is in the server side or in your application side.

Let me know if it works