1

I got some code for Twitter update and search api from CodeProject which worked just fine.

However, i get 401 when i tried to convert it for the stream api: "https://stream.twitter.com/1/statuses/filter.json". I will appreciate any help. See code below:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Security.Cryptography;
using System.IO;

public class OAuthRead
{
    public static void Authenticate()
    {
        // oauth application keys
        var oauth_consumer_key = "****";
        var oauth_consumer_secret = "*****";
        var oauth_token = "******";
        var oauth_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();

        //var resource_url = "https://stream.twitter.com/1/statuses/filter.json?track=obama&include_entities=1&language=en";
        //var resource_url = "https://api.twitter.com/1.1/statuses/user_timeline.json";
        var resource_url = "https://stream.twitter.com/1.1/statuses/filter.json";
        //var screen_name = "femitfash";
        // 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}";

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

        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}\"";

        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)
        );


        // make the request

        ServicePointManager.Expect100Continue = false;

        var postBody = "track=obama&include_entities=1&language=en"; // "screen_name=" + Uri.EscapeDataString(screen_name);//
        resource_url += "?" + postBody;
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(resource_url);
        request.Headers.Add("Authorization", authHeader);
        request.Method = "POST";
        request.ContentType = "application/x-www-form-urlencoded";
        request.PreAuthenticate = true;
        request.AllowWriteStreamBuffering = true;

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

    }
}
3

try include your query string to baseString:

var baseFormat = "oauth_consumer_key={0}&oauth_nonce={1}&oauth_signature_method={2}" +
                  "&oauth_timestamp={3}&oauth_token={4}&oauth_version={5}&track={6}";
var baseString = string.Format(baseFormat,
                                    oauth_consumer_key,
                                    oauth_nonce,
                                    oauth_signature_method,
                                    oauth_timestamp,
                                    oauth_token,
                                    oauth_version,
                                    Uri.EscapeDataString(valueToTrack)
                                    );

....

var postBody = "track=obama";

https://dev.twitter.com/docs/auth/creating-signature says: 5. Percent encode the parameter string and append it to the output string.

  • That did it, you really deserve a beer! – user2482206 Jun 15 '13 at 0:07

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.