Question

Since twitter is depreciating API version 1 soon I've decided to convert an older Application to allow it to work with the new 1.1 API. From what I know about 1.1 I know you have to authenticate before making a call and use JSON rather than RSS for serializing the data. The application is WPF coded using xmal and c#

I am able to successfully authenticate using the LINQ to Twitter Library but I am lost when it comes to using JSON. Here is my code that I used for API v1

 else if (auth.IsAuthorized && i == 2)
        {
            SyndicationClient client = new SyndicationClient();
            SyndicationFeed feed = await client.RetrieveFeedAsync(new Uri("https://api.twitter.com/1/statuses/user_timeline.rss?screen_name=ScreenName"));
            {
                _model.Tweets.Clear();
                foreach (var item in feed.Items)
                {
                    _model.Tweets.Add(new Tweet
                    {
                        Name = "@ExampleHandle",
                        Message = item.Title.Text,
                        Image = new BitmapImage(new Uri("ms-appx:Assets/test_image", UriKind.RelativeOrAbsolute)),
                    });
                }
            }

        }
    }

And here is the code for the tweet class

 public class Tweet
{
    public String Name { get; set; }
    public String Message { get; set; }
    public ImageSource Image { get; set; }
}

I was wondering if someone could point me in the right direction for writing the JSON equivalent of this. Thanks in advance!

Was it helpful?

Solution

For those you read this question later on I was able to solve this problem. Below are my answers depending on your situation.

If you simply want to use Json instead of RSS you can do it like this:

   HttpClient client = new HttpClient();
            HttpResponseMessage response = await client.GetAsync(new Uri("https://api.twitter.com/1/statuses/user_timeline.json?screen_name=ScreenName"));
            string ApiResponse = await response.Content.ReadAsStringAsync();

            List<Tweet> tweets = await JsonConvert.DeserializeObjectAsync<List<Tweet>>(ApiResponse);
            _model.Tweets.Clear();
            foreach (var item in tweets)
            {
                _model.Tweets.Add(new Tweet
                {
                    Name = "@UserName",
                    Message = item.Text,
                    Image = new BitmapImage(new Uri("ms-appx:Assets/sampleLocalImage", UriKind.RelativeOrAbsolute)),
                });

However because of API 1.1 you must be authenticated before EACH call to the API for this is used Linq to Twitter. Here is the code for Authorization:

   var auth = new SingleUserAuthorizer
        {
            Credentials = new InMemoryCredentials
            {
                ConsumerKey = TwitterSettings.ConsumerKey,
                ConsumerSecret = TwitterSettings.ConsumerKeySecret,
                OAuthToken = TwitterSettings.AccessToken,
                AccessToken = TwitterSettings.AccessTokenSecret,
            }
        };
        auth.Authorize();

And the Code to Perform a Search(This is the code you want to use if using Twitter API 1.1):

 var twitterCtx = new TwitterContext(auth);

            var statusTweets =
           from tweet in twitterCtx.Status
           where tweet.Type == StatusType.User
                 && tweet.ScreenName == "ScreenName"
           select tweet;


            _model.Tweets.Clear();
            foreach (var item in statusTweets)
            {
                _model.Tweets.Add(new Tweet
             {
                 Name = item.User.Name,
                 Message = item.Text,
                 Image = new BitmapImage(new Uri(item.User.ProfileImageUrl)),
             });

OTHER TIPS

I'm not familiar with the Twitter API, but I would assume some combination of HttpClient (if you're on .NET 4.0, you can get it here) and Newtonsoft.Json would be appropriate.

Newtonsoft.Json is not authored by Microsoft, but it is the package that everyone uses (including Microsoft's default web templates). The old Microsoft JSON serialization stuff is pretty much dead at this point.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top