关于无法使用Twitter开放api的问题?

最近需要做一个安卓app,主要实现获取他人@我的信息。
但是我遇到了一个问题。困扰了很久。
如果曾经做过,请帮助我一下。感激不尽!
我的QQ:514864610 多谢指教!多谢帮助!
我按照推特的要求,去申请了access_token:图片描述

随后我使用这个token,去实现对应的api,但是有些api请求到数据了:
图片描述
但是有些api缺并没有请求到数据,而是返回了:
图片描述
这是为什么呢?
根据官方的文档:
Bearer token used on endpoint which doesn’t support application-only auth
Requesting an endpoint which requires a user context (such as statuses/home_timeline) with a bearer token will produce:

HTTP/1.1 403 Forbidden
Content-Type: application/json; charset=utf-8
Content-Length: 91
...

{"errors":[{"message":"Your credentials do not allow access to this resource","code":220}]}

我不知道是否能放链接。放一个试试吧。这是我需要实现的请求
https://dev.twitter.com/rest/...
这是一个可以实现的请求:https://api.twitter.com/1.1/u...
附上一份我请求的代码
private void sendRequestWithHttpURLConnection() {

new Thread(new Runnable() {

@Override

public void run() {

HttpURLConnection connection = null;

URL url = null;

try {

url = new URL("https://api.twitter.com/1.1/statuses/mentions_timeline.json?count=2&since_id=14927799");

connection = (HttpURLConnection) url.openConnection();

connection.setRequestMethod("GET");

connection.setRequestProperty("Authorization","Bearer AAAAAAAAAAAAAAAAAAAAALrTxwAAAAAAYRuvnIXWe4pA8gPhRAuVsX2ND94%3DjcGVKktWDmLtCGwFYDoxayrqXwui6xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"这些是根据推特要求编码生成);

//详见https://dev.twitter.com/oauth/application-only

connection.setConnectTimeout(8000);

connection.setReadTimeout(8000);

// connection.connect();

                    InputStream in = connection.getInputStream();

BufferedReader reader = new BufferedReader(new InputStreamReader(in));

StringBuilder response = new StringBuilder();

String line;

while ((line = reader.readLine()) != null) {

response.append(line);

}

Message message = new Message();

message.what = SHOW_RESPONSE;

//将服务器返回的结果存放到Message中

message.obj = response.toString();

Log.d("Twitter","response.toString()="+response.toString());

handler.sendMessage(message);

} catch (Exception e) {

e.printStackTrace();

}finally {

if (connection!=null){

connection.disconnect();

}

}

}

}).start();

}

回答:

楼主还是直接使用twitter4j吧,你遇到的问题是因为验证方式不正确,你使用的是Application Only Auth而该接口需要的是User Auth
图片描述

示例

ConfigurationBuilder cb = new ConfigurationBuilder();

cb.setDebugEnabled(true)

.setOAuthConsumerKey("*")

.setOAuthConsumerSecret("*")

.setOAuthAccessToken("*")

.setOAuthAccessTokenSecret("*");

Twitter twitter = new TwitterFactory(cb.build()).getInstance();

User user = twitter.verifyCredentials();

List<Status> statuses = twitter.getMentionsTimeline();

System.out.println("Showing @" + user.getScreenName() + "'s mentions.");

for (Status status : statuses) {

System.out.println("@" + status.getUser().getScreenName() + " - " + status.getText());

}

https://github.com/yusuke/twi...

以上是 关于无法使用Twitter开放api的问题? 的全部内容, 来源链接: utcz.com/p/174001.html

回到顶部