How to create an authorized create welcome message request using C# and twitter DM API

directmessages
csharp
welcomemessage

#1

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 :

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;

    }

#2

I’m not sure what the issue is here, but one suggestion I have would be to try doing the same call using twurl with the -t trace flag, and compare the HTTP headers and request with your code’s output. I’ve got an example set of commands here.


#3

hi @andypiper , i am actually working with c#.
When i send GET request i get correct response for exemple :
GET direct_messages/events/list i can get the list of messages.
But when i try to make POST requests like POST direct_messages/welcome_messages/new i get UNAUTHORIZED error


#4

Yes, I understand this is C#, and I’m not such a strong coder in that language. If you are able to execute an API call using twurl -t you should see the headers that are generated on the POST request, and then be able to compare them with what your code is doing.