Question

In my Android application, have integrated Twitter.

I have changed the user/show api version 1 to version 1.1. The new api is not working and it gives 400 - Bad request exception. I have spend a lot of time to resolve but unable to find out the solution.

version 1 api for user/show:

"https://api.twitter.com/1/users/show.json?user_id="+userID+"&include_entities=true"

version 1.1 api for user/show:

"https://api.twitter.com/1.1/users/show.json?user_id="+userID+"&include_entities=true"

I have tried without include_entities=trueand include_entities=false.

Referred the twitter developer site: enter link description here

In my code I am using http GET method to get json response from the api.

code sample:

        StringBuilder builder = new StringBuilder();
        HttpClient client = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(params[0]);
        try {
          HttpResponse response = client.execute(httpGet);
          StatusLine statusLine = response.getStatusLine();
          int statusCode = statusLine.getStatusCode();
          if (statusCode == 200) {
                      ----------
          } else {
            Log.e("Server code ", statusCode + " * " + statusLine+" Failed to download");
          }

Exception I get:
Server code(460): 400 * HTTP/1.1 400 Bad Request Failed to download

please help me to solve the problem.

Was it helpful?

Solution

I have solved my problem by sending authentication with api.

In api version 1.1 we need to send authentication with request otherwise we get 400 bad request(invalid request).

This is the coding example for sending authentication.

private OAuthConsumer consumer;

consumer = new CommonsHttpOAuthConsumer(
Constants.CONSUMER_KEY, Constants.CONSUMER_SECRET);

consumer.setTokenWithSecret(token,secret_token);

..........

HttpGet httpGet = new HttpGet(params[0]);
consumer.sign(hpost);

we need sign-core and sign common post jars.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top