Java 检索给定用户的列表';使用Twitter API 1.1发布推文并进行改进
我试图获取用户的tweet列表,但在尝试验证对API的调用时遇到了一些问题。我目前在执行以下代码时得到401:Java 检索给定用户的列表';使用Twitter API 1.1发布推文并进行改进,java,android,twitter-oauth,retrofit,signpost,Java,Android,Twitter Oauth,Retrofit,Signpost,我试图获取用户的tweet列表,但在尝试验证对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 SEC
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);
}
我还包括了路标改造插件中的相关代码:
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);
}
}
路标改装插件可在此处找到:
这里的任何帮助都会很好。解决方案不必包括使用路标,但我确实希望使用改装。我也不想在网络视图中向用户显示“使用Twitter验证”屏幕-我只想在详细视图中显示一些相关tweet。你确定路标改造项目适用于Twitter oauth吗?我在过去成功地使用了twitter4j——如果您不想要完整的库,可以使用它们的代码作为参考 根据这一要点,它确实有效:您从哪里获得身份验证令牌??
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);
}
}
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);
}
}