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

 汽车之家马甲小宝宝_457 发布于 2022-10-26 07:50

最近需要做一个安卓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...

    2022-10-27 00:37 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有