0

I need to obtain several pieces of information from a tweet using the json twitter API.

The only source provided is a tweet link.

This solution requires no twitter authentication and only 1 library json.net (available in Nuget Package Manager)

So far:

  • The program is able to do a httprequest on the Twitter json API.
  • I can see a response in the stream
  • But I am not able to deserialize the json response into the class.

Where:

  • Place a breakpoint in line and You will see that json_data contains json.

MyTweet = JsonConvert.DeserializeObject(json_data);

using System;
using System.IO;
using System.Net;
using Newtonsoft.Json;

namespace parsejson
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {

                Tweet MyTweet = new Tweet();

                string url;
                url = "https://api.twitter.com/1/statuses/oembed.json?url=https://twitter.com/katyperry/status/657469411700244480";
                var json_data = string.Empty;

                HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
                req.Method = WebRequestMethods.Http.Get;
                req.Accept = "application/json";
                req.ContentType = "applicaton/json;charset=utf-8";
                WebResponse resp = req.GetResponse();

                Stream stream = resp.GetResponseStream();
                StreamReader sr = new StreamReader(stream);

                json_data = sr.ReadToEnd();
                MyTweet = JsonConvert.DeserializeObject<Tweet>(json_data);
                var stext = MyTweet.text;
                Console.ReadLine();

            }
            catch (Exception)
            {

                throw;
            }
        }
        public class Tweet
        {
            public string created_at { get; set; }
            public long id { get; set; }
            public string id_str { get; set; }
            public string text { get; set; }
            public string source { get; set; }
        }
    }
}
Internet Engineer
  • 2,514
  • 8
  • 41
  • 54
  • Possible duplicate of [Deserialize tweets returned from twitter api 1.1 c#](http://stackoverflow.com/questions/29887709/deserialize-tweets-returned-from-twitter-api-1-1-c-sharp) – MethodMan Oct 26 '15 at 15:31
  • I tried that solution and did not work for me – Internet Engineer Oct 26 '15 at 15:32
  • 1
    You should be using `v1.1`. I'm actually surprised `api.twitter.com/1/*` works (given it was supposed to be [killed off in 2013](https://blog.twitter.com/2013/api-v1-retirement-final-dates)) – Brad Christie Oct 26 '15 at 15:37
  • GET /1.1/statuses/oembed.json?align=left&url=https://twitter.com/katyperry/status/657469411700244480 HTTP/1.1 Authorization: OAuth oauth_consumer_key="xxxxx",oauth_signature_method="HMAC-SHA1",oauth_timestamp="xxx",oauth_nonce="-396660717",oauth_version="1.0",oauth_token="xxxx",oauth_signature="xxxx" Host: api.twitter.com X-Target-URI: https://api.twitter.com Connection: Keep-Alive – Internet Engineer Oct 26 '15 at 15:54
  • I did not know to use 1.1. The official example states v1. – Internet Engineer Oct 26 '15 at 15:55

0 Answers0