How to allow anonymous twitter connections in an MVC configuration

前端 未结 1 1764
孤城傲影
孤城傲影 2021-01-07 15:24

Using - spring-social 1.1.4 - spring-social-twitter 1.1.2 - spring-mvc-4.2 - on tomcat 7

Below is my tomcat config for my MVC Application including twitter set

相关标签:
1条回答
  • 2021-01-07 15:37

    I see that you have twitterTemplate bean configured, if you are able to use it in the application you won't need to do anything else.

    Use TwitterTemplate instead of Twitter. TwitterTemplate implements Twitter interface, so all methods are available.

    Examples:

    //1. Search Twitter
    SearchResults results = twitterTemplate.searchOperations().search("#WinterIsComing");
    List<Tweet> tweets = results.getTweets();
    int i =0;
    for (Tweet tweet : tweets) {
        System.out.println(tweet.getUser().getName() + " Tweeted : "+tweet.getText() + " from " + tweet.getUser().getLocation() 
                + " @ " + tweet.getCreatedAt() + tweet.getUser().getLocation()  );
    }
    
    //2. Search Place by GeoLocation        
    RestTemplate restTemplate = twitterTemplate.getRestTemplate();
    GeoTemplate geoTemplate = new GeoTemplate(restTemplate, true, true);
    List<Place> place = geoTemplate.search(37.423021, -122.083739);
    for (Place p : place) {
        System.out.println(p.getName() + " " + p.getCountry() + " "+p.getId());
    }           
    
    //3. Get Twitter UserProfile
    TwitterProfile userProfile = twitterTemplate.userOperations().getUserProfile();
    System.out.println(userProfile.getName()+" has " +userProfile.getFriendsCount() + " friends");
    

    Using TwitterTemplate, users don't need to login. Hope this helps!

    0 讨论(0)
提交回复
热议问题