Post reply with in_reply_to_status_id throws The remote server returned an error: (401) Unauthorized


#1

Hello I am trying to post reply against status but it throwing “The remote server returned an error: (401) Unauthorized.” error. My code is working fine if I post a new status.

Here is my code:

    private string PostTwitterMessage(string message)
    {

        var token = "MyToken";
        var token_secret = "Token_secret";
        var oauth_consumer_key = "Key";
        var oauth_consumer_secret = "secret";
        string PostID = string.Empty;
        try
        {
            // oauth application keys
            var oauth_token = token;
            var oauth_token_secret = token_secret;

            // oauth implementation details
            var oauth_version = "1.0";
            var oauth_signature_method = "HMAC-SHA1";

            // unique request details
            var oauth_nonce = Convert.ToBase64String(
                new ASCIIEncoding().GetBytes(DateTime.Now.Ticks.ToString()));
            var timeSpan = DateTime.UtcNow
                - new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc );
            var oauth_timestamp = Convert.ToInt64(timeSpan.TotalSeconds).ToString();

            // message api details
            var status = message;
            var resource_url = "https://api.twitter.com/1.1/statuses/update.json";

            // create oauth signature

            var baseFormat = "oauth_consumer_key={0}&oauth_nonce={1}&oauth_signature_method={2}" +
                        "&oauth_timestamp={3}&oauth_token={4}&oauth_version={5}&status={6}";

            var baseString = string.Format(baseFormat,
                                        oauth_consumer_key,
                                        oauth_nonce,
                                        oauth_signature_method,
                                        oauth_timestamp,
                                        oauth_token,
                                        oauth_version,
                                         Uri.EscapeDataString(status)
                                        );


            baseString = string.Concat("POST&", Uri.EscapeDataString(resource_url), "&", Uri.EscapeDataString("in_reply_to_status_id=346397028374425602"), "&", Uri.EscapeDataString(baseString));




            var compositeKey = string.Concat(Uri.EscapeDataString(oauth_consumer_secret),
                             "&", Uri.EscapeDataString(oauth_token_secret));

            string oauth_signature;
            using (HMACSHA1 hasher = new HMACSHA1(ASCIIEncoding.ASCII.GetBytes(compositeKey)))
            {
                oauth_signature = Convert.ToBase64String(
                    hasher.ComputeHash(ASCIIEncoding.ASCII.GetBytes(baseString)));
            }

            // create the request header

      var      headerFormat = "OAuth oauth_consumer_key=\"{0}\", oauth_nonce=\"{1}\", oauth_signature=\"{2}\", oauth_signature_method=\"{3}\", oauth_timestamp=\"{4}\", oauth_token=\"{5}\", oauth_version=\"{6}\"";



          var  authHeader = string.Format(headerFormat,
               Uri.EscapeDataString(oauth_consumer_key),
                                    Uri.EscapeDataString(oauth_nonce),
                                     Uri.EscapeDataString(oauth_signature),
                                    Uri.EscapeDataString(oauth_signature_method),
                                    Uri.EscapeDataString(oauth_timestamp),
                                    Uri.EscapeDataString(oauth_token),
                                    Uri.EscapeDataString(oauth_version)
                            );


            // make the request
            var postBody = "status=" + Uri.EscapeDataString(status) + "&" + "in_reply_to_status_id=" + Uri.EscapeDataString("346397028374425602");
          
            ServicePointManager.Expect100Continue = false;

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(resource_url);
            request.Headers.Add("Authorization", authHeader);
            request.Method = "POST";
            request.Timeout = 120000;
            request.ContentType = "application/x-www-form-urlencoded";
            using (Stream stream = request.GetRequestStream())
            {
                byte[] content = ASCIIEncoding.ASCII.GetBytes(postBody);
                stream.Write(content, 0, content.Length);
            }

            var response = (HttpWebResponse)request.GetResponse();
            if (response.StatusCode != HttpStatusCode.OK)
                Console.WriteLine("There's been a problem trying to tweet:" +
                                Environment.NewLine +
                                response.StatusDescription);
            else
            {
                PostID = "Successful";
            }
        }
        catch (Exception exp)
        {
            return exp.Message;
        }
        return PostID;
    }

This code runs fine if i not include reply Id with it. I post my message start with @ScreenName.

Regards


#2

Hi Waqar,

I am getting the same error. Did you resolve the issue?

Regards
Arun


#3

Hi Waqar,

I am getting the same error. Did you resolve the issue?

Regards
Anu