【问题标题】:Retrieve a list of a given user's tweets using Twitter API 1.1 and Retrofit使用 Twitter API 1.1 和 Retrofit 检索给定用户的推文列表
【发布时间】:2015-05-05 21:21:33
【问题描述】:

我正在尝试获取用户的推文列表,但在尝试验证我对 API 的调用时遇到了一些麻烦。我目前在执行以下代码时得到 401:

    public interface TwitterApi {

    String API_URL = "https://api.twitter.com/1.1";

    String CONSUMER_KEY = "<CONSUMER KEY GOES HERE>";
    String CONSUMER_SECRET = "<CONSUMER SECRET GOES HERE>";
    String ACCESS_TOKEN = "<ACCESS TOKEN GOES HERE>";
    String ACCESS_TOKEN_SECRET = "<ACCESS TOKEN SECRET GOES HERE>";

      @GET("/statuses/user_timeline.json")
      List<Tweet> fetchUserTimeline(
            @Query("count") final int count,
            @Query("screen_name") final String screenName);
      }

以下调用fetchUserTimeline()时抛出401授权错误

RetrofitHttpOAuthConsumer consumer = new RetrofitHttpOAuthConsumer(TwitterApi.CONSUMER_KEY, TwitterApi.CONSUMER_SECRET);
        consumer.setTokenWithSecret(TwitterApi.ACCESS_TOKEN, TwitterApi.ACCESS_TOKEN_SECRET);
        RestAdapter restAdapter = new RestAdapter.Builder()
                .setEndpoint(TwitterApi.API_URL)
                .setClient(new SigningOkClient(consumer))
                .build();


        TwitterApi twitterApi = restAdapter.create(TwitterApi.class)

        tweets = twitterApi.fetchUserTimeline(2, screenName);

我还包含了路标改造插件中的相关代码:

public class SigningOkClient extends OkClient {

    private final RetrofitHttpOAuthConsumer mOAuthConsumer;

    public SigningOkClient(RetrofitHttpOAuthConsumer consumer) {
        mOAuthConsumer = consumer;
    }

    public SigningOkClient(OkHttpClient client, RetrofitHttpOAuthConsumer consumer) {
        super(client);
        mOAuthConsumer = consumer;
    }

    @Override
    public Response execute(Request request) throws IOException {
        Request requestToSend = request;
        try {
            HttpRequestAdapter signedAdapter = (HttpRequestAdapter) mOAuthConsumer.sign(request);
            requestToSend = (Request) signedAdapter.unwrap();
        } catch (OAuthMessageSignerException | OAuthExpectationFailedException | OAuthCommunicationException e) {
            // Fail to sign, ignore
            e.printStackTrace();
        }
        return super.execute(requestToSend);
    }

}

signpost-retrofit 插件可以在这里找到:https://github.com/pakerfeldt/signpost-retrofit

public class RetrofitHttpOAuthConsumer extends AbstractOAuthConsumer {

  private static final long serialVersionUID = 1L;

  public RetrofitHttpOAuthConsumer(String consumerKey, String consumerSecret) {
    super(consumerKey, consumerSecret);
  }

  @Override
  protected HttpRequest wrap(Object request) {
      if (!(request instanceof retrofit.client.Request)) {
        throw new IllegalArgumentException("This consumer expects requests of type " + retrofit.client.Request.class.getCanonicalName());
      }
      return new HttpRequestAdapter((Request) request);
  }
}

这里的任何帮助都会很棒。该解决方案没有 包括使用路标,但我确实想使用 Retrofit。我也不想希望在 WebView 中向用户显示“使用 Twitter 进行身份验证”屏幕 - 我只是想在详细视图中显示一些相关的推文。

【问题讨论】:

  • 您从哪里获得身份验证令牌??

标签: java android twitter-oauth retrofit signpost


【解决方案1】:

您确定 signpost-retrofit 项目适用于 twitter oauth 吗?我过去曾成功使用过 twitter4j - 如果您不想要完整的库,您可以使用他们的代码作为参考。 twitter4j

【讨论】:

猜你喜欢
  • 2016-12-19
  • 2013-06-09
  • 2012-12-05
  • 1970-01-01
  • 2013-08-19
  • 1970-01-01
  • 1970-01-01
  • 2014-02-15
  • 2013-06-08
相关资源
最近更新 更多