java - 关于无法使用Twitter开放api的问题?
天蓬老师
天蓬老师 2017-04-18 10:23:08
[Java讨论组]

最近需要做一个安卓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();
    }
天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

全部回复(1)
巴扎黑

楼主还是直接使用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...

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号