Android twitter user/show (user information) migration version 1 to 1.1

戏子无情 提交于 2019-12-13 03:36:22

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/17277145/android-twitter-user-show-user-information-migration-version-1-to-1-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!