Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am working with Twitter AccountActivity API and DM API but when it comes to POST/DELETE requests, i get this error :
401 Unauthorized
But the GET requests just work fine. I think i have something wrong with the request header. Could someone please help? This is a sample of the code i am using :

What I have tried:

<pre lang="c#"><pre>public WebResponse PostWelcomeMessageRule(HttpWebRequest request)
    {
        var oauth_token = ConfigurationManager.AppSettings["TK"];
        var oauth_token_secret = ConfigurationManager.AppSettings["TS"];
        var oauth_consumer_key = ConfigurationManager.AppSettings["CK"].ToString();
        var oauth_consumer_secret = ConfigurationManager.AppSettings["CS"].ToString();
        // oauth implementation details
        var oauth_version = "1.0";
        var oauth_signature_method = "HMAC-SHA1";
        var welcomeMsgId = 961185762921328645;
        // 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 resource_url = "https://api.twitter.com/1.1/direct_messages/welcome_messages/rules/new.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}&welcome_message_id={6}";


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

        baseString = string.Concat("POST&", Uri.EscapeDataString(resource_url), "&", 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_nonce=\"{0}\", oauth_signature_method=\"{1}\", " +
                           "oauth_timestamp=\"{2}\", oauth_consumer_key=\"{3}\", " +
                           "oauth_token=\"{4}\", oauth_signature=\"{5}\", " +
                           "oauth_version=\"{6}\", welcome_message_id=\"{7}\"";

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

        request = (HttpWebRequest)WebRequest.Create(resource_url);
        request.Headers.Add("Authorization", authHeader);
        request.Method = "POST";
        request.ContentType = "application/json; charset=utf-8";

        WebResponse response = request.GetResponse();
        string responseData = new StreamReader(response.GetResponseStream()).ReadToEnd();
        return response;

    }
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900