Select user_timeline By Twitter API



Hello All,

Today i have the problem when try to get data “user_timeline” from Twitter, I alway get the error “The remote server returned an error: (401) Unauthorized.”, below is my code.

//Oauth Keys (Replace with values that are obtained from registering the application
            var oauth_consumer_key = "OI9xxxxxxeUZ";
            var oauth_consumer_secret = "axxxxx0";

            //Token URL
            var oauth_url = "";
            var headerFormat = "Basic {0}";
            var authHeader = string.Format(headerFormat,
            Convert.ToBase64String(Encoding.UTF8.GetBytes(Uri.EscapeDataString(oauth_consumer_key) + ":" + Uri.EscapeDataString((oauth_consumer_secret)))));

            var postBody = "grant_type=client_credentials";

            ServicePointManager.Expect100Continue = false;
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(oauth_url);
            request.Headers.Add("Authorization", authHeader);
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded;charset=UTF-8";

            using (Stream stream = request.GetRequestStream())
                byte[] content = ASCIIEncoding.ASCII.GetBytes(postBody);
                stream.Write(content, 0, content.Length);

            //create our request
            System.Net.HttpWebRequest request1 = (HttpWebRequest)System.Net.WebRequest.Create("");

            //set our info
            // request.Headers.Add("Authorization", authHeader);
            request1.Method = "GET";
            request1.ContentType = "application/x-www-form-urlencoded";

            //get the response and return the result from the stream
            using (var response1 = (System.Net.HttpWebResponse)request1.GetResponse())
                using (var reader = new System.IO.StreamReader(response1.GetResponseStream()))


I’ve edited your keys out of this post - it really isn’t a good idea to let other people see those.

  • Are there any timing issues on your machine? if the clock is not set
    accurately, you can see OAuth problems.
  • are you able to make the same call with those keys using [twurl][1]?
  • Have you considered using a
    pre-built third-party library to help you here?