Android twitter用户/ show(用户信息)迁移版本1到1.1

时间:2013-06-24 13:49:09

标签: android twitter android-twitter

在我的Android应用程序中,集成了Twitter。

我已将用户/ show api版本1更改为1.1版。新的api不起作用,它给出400 - Bad请求异常。我花了很多时间来解决但无法找到解决方案。

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"

我试过没有include_entities=trueinclude_entities=false

推荐了Twitter开发者网站: enter link description here

在我的代码中,我使用http GET方法从api获取json响应。

代码示例:

        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

请帮我解决问题。

1 个答案:

答案 0 :(得分:0)

我通过api发送身份验证解决了我的问题。

在api版本1.1中,我们需要发送带有请求的身份验证,否则我们会收到400个错误请求(无效请求)。

这是发送身份验证的编码示例。

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);

我们需要签名核心并签署常见的邮政罐。