【问题标题】:Using Twitter Fabric to retrieve my own account tweets使用 Twitter Fabric 检索我自己的帐户推文
【发布时间】:2015-06-16 11:29:00
【问题描述】:

我目前正在使用 twitter 结构框架并尝试检索我自己的帐户推文列表。我尝试在线搜索无济于事。文档中的示例展示了如何根据 tweetID 显示推文。我不要那个。我确实了解 REST 客户端与提供的变量建立 http 连接,并将 JSON 结果检索回解析等。

这是我当前的代码,成功登录后它将显示另一个活动,我希望在此处显示我的推文。

          public void success(Result<TwitterSession> result) {
            // Do something with result, which provides a TwitterSession for making API calls
            TwitterSession session = Twitter.getSessionManager().getActiveSession();
            TwitterAuthToken authToken = session.getAuthToken();
            token = authToken.token;
            secret = authToken.secret;
            Log.i("token",token);
            Log.i("secret",secret);
            successNewPage();
        }

我还使用意图将令牌和密钥传递给下一个活动

   public void successNewPage(){
    Intent intent = new Intent(this, LoginSuccess.class);
    intent.putExtra("token",token);
    intent.putExtra("secret", secret);
    startActivity(intent);
}

在新的活动课上,我按照他们的文档提出了这个,

  TwitterAuthConfig authConfig =  new TwitterAuthConfig("consumerKey", "consumerSecret");
    Fabric.with(this, new TwitterCore(authConfig), new TweetUi());

    TwitterCore.getInstance().logInGuest(new Callback() {

        public void success(Result appSessionResult) {

            //Do the rest API HERE
            Bundle extras = getIntent().getExtras();
            String bearerToken = extras.getString("token");
            try {
                fetchTimelineTweet(bearerToken);
            } catch (IOException e) {
                e.printStackTrace();
            }

        }


        public void failure(TwitterException e) {
            Toast.makeText(getApplicationContext(), "Failure =)",
                    Toast.LENGTH_LONG).show();
        }

    });
}

推文的检索将是:

 // Fetches the first tweet from a given user's timeline
private static String fetchTimelineTweet(String endPointUrl)
        throws IOException {
    HttpsURLConnection connection = null;



    try {
        URL url = new URL(endPointUrl);
        connection = (HttpsURLConnection) url.openConnection();
        connection.setDoOutput(true);
        connection.setDoInput(true);
        connection.setRequestMethod("GET");
        connection.setRequestProperty("Host", "api.twitter.com");
        connection.setRequestProperty("User-Agent", "anyApplication");
        connection.setRequestProperty("Authorization", "Bearer " +  endPointUrl);
        connection.setUseCaches(false);


        String res = readResponse(connection);
        Log.i("Response", res);
        return new String();
    } catch (MalformedURLException e) {
        throw new IOException("Invalid endpoint URL specified.", e);
    } finally {
        if (connection != null) {
            connection.disconnect();
        }
    }
}

我在日志中得到的是:

380-380/com.example.john.fabric W/System.err: java.io.IOException: 指定的端点 URL 无效。

我的网址错了吗?还是我在 endpointURL 中设置的令牌也错误? 任何建议将不胜感激。谢谢!

【问题讨论】:

    标签: android twitter twitter-fabric


    【解决方案1】:

    应该是这样,消息是由fetchTimelineTweet 函数抛出的。这应该是由这一行引起的:URL url = new URL(endPointUrl); 告诉endPointUrl 导致MalformedURLException

    编辑

    根据Twitter Dev Page,你应该把它作为endpointURL : https://dev.twitter.com/rest/reference/get/statuses/user_timeline 并将用户屏幕名作为参数传递。

    编辑 2

    我认为你的代码应该是这样的:

    private static String fetchTimelineTweet(String endPointUrl, String token) // this line
            throws IOException {
        HttpsURLConnection connection = null;
    
        try {
            URL url = new URL(endPointUrl);
            connection = (HttpsURLConnection) url.openConnection();
            connection.setDoOutput(true);
            connection.setDoInput(true);
            connection.setRequestMethod("GET");
            connection.setRequestProperty("Host", "api.twitter.com");
            connection.setRequestProperty("User-Agent", "anyApplication");
            connection.setRequestProperty("Authorization", "Bearer " +  token); // this line
            connection.setUseCaches(false);
    
    
            String res = readResponse(connection);
            Log.i("Response", res);
            return new String();
        } catch (MalformedURLException e) {
            throw new IOException("Invalid endpoint URL specified.", e);
        } finally {
            if (connection != null) {
                connection.disconnect();
            }
        }
    }
    

    【讨论】:

    • 在这种情况下我应该放什么?谢谢!
    • 我传入了身份验证后获得的令牌。我认为这里有问题。
    猜你喜欢
    • 2012-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-05
    • 2020-04-01
    • 2016-12-19
    • 1970-01-01
    • 1970-01-01
    • 2019-03-06
    相关资源
    最近更新 更多