0

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

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

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=trueand include_entities=false

参考 twitter 开发者网站: 在此处输入链接描述

在我的代码中,我使用 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

请帮我解决问题。

4

1 回答 1

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

我们需要 sign-core 并签署常见的 post jars。

于 2013-06-24T15:52:23.357 回答