Using Twitter Fabric to retrieve my own account tweets

217 views Asked by At

I'm currently using twitter fabric framework and trying to retrieve the list of my own account tweets. I tried searching online to no avail. The example in the document shows how to display a tweet based on tweetID. I do not want that. I do understand that REST client makes a http connection with the supplied variables and to retrieve the JSON results back to parse and etc.

This are my current codes which upon successful login it will display another activity which i want my tweets to be shown here.

          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();
        }

I have also passed the token and secret key over to the next activity using intent

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

On the new activity class, i followed their documentation and came out with this,

  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();
        }

    });
}

And the retrieve of tweets would be:

 // 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();
        }
    }
}

What i get in my log is:

380-380/com.example.john.fabric W/System.errīš• java.io.IOException: Invalid endpoint URL specified.

Is my url wrong? Or is the token which I set in the endpointURL wrong too? Any advice would be greatly appreciated. Thanks!

1

There are 1 answers

2
reidzeibel On BEST ANSWER

That should be the case, the message was thrown by the fetchTimelineTweet function. That should be caused by this line : URL url = new URL(endPointUrl); which tells that the endPointUrl causes MalformedURLException

EDIT :

According to the Twitter Dev Page, you should put this as endpointURL : https://dev.twitter.com/rest/reference/get/statuses/user_timeline and pass the user screenname as parameter.

EDIT 2 :

I think your code should be like this :

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();
        }
    }
}