Search code examples
javascripttwittertwitter-oauthtwitter-anywhere

Getting tweets via @Anywhere JavaScript API


I'm giving the user the opportunity to log in via OAuth and the Twitter JavaScript api and I would like to display all of the users tweets. I can't find an equivalent to Facebooks FB.api('/me/feed') in the documentation to @Anywhere.


Solution

  • @Anywhere propagates a logged in user object.

    When the user is connected the currentUser property can be used to retrieve information about the logged-in user. The user object has a data method that can be passed a string representing the property to retrieve.

    Here you can query for an ID which you can pass on to the REST API

    <span id="twitter-connect-placeholder"></span>
    <script type="text/javascript">
    
      twttr.anywhere(function (T) {
    
        var currentUser,
            screenName;
    
        if (T.isConnected()) {
          currentUser = T.currentUser;
          screenName = currentUser.data('screen_name');
          $.getJSON('http://api.twitter.com/1/users/show.json?callback=?',
          {
          screen_name: screenName //pass @Anywhere screenname here
      },
     function (data) {
          console.log(data.status); //get last tweet from the user
      });    
        }
    
      });
    
    </script>