Problem while using login with Twitter


#1

I need to add a button ‘Sign in with Twitter’ to my website.
I created an app and created access token. I checked the option “Allow this application to be used to Sign in with Twitter” in the settings.
Then I went through this tutorial: https://dev.twitter.com/docs/auth/implementing-sign-twitter

This is my C# code for “Step 1: Obtaining a request token”:

        var oauth_token_secret = "AAA";
        var oauth_consumer_key = "AAA";
        var oauth_consumer_secret = "AAA";

        // 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://api.twitter.com/oauth/request_token";

        var oauth_callback = "http://localhost:1532/OAuthDemo/DefaultTwitter.aspx";

        // create oauth signature
        var baseFormat = "oauth_consumer_key={0}&oauth_nonce={1}&oauth_signature_method={2}" +
        "&oauth_timestamp={3}&oauth_consumer_key={4}&oauth_version={5}";

        var baseString = string.Format(baseFormat,
        oauth_consumer_key,
        oauth_nonce,
        oauth_signature_method,
        oauth_timestamp,
        oauth_consumer_key,
        oauth_version
        );
        
        baseString = string.Concat("POST&", Uri.EscapeDataString(resource_url), "&", Uri.EscapeDataString(baseString));

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

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

        // create the request header
        var headerFormat = "OAuth oauth_callback=\"{0}\", oauth_consumer_key=\"{1}\", oauth_nonce=\"{2}\", oauth_signature=\"{3}\"," +
        " oauth_signature_method=\"{4}\", oauth_timestamp=\"{5}\", oauth_version=\"{6}\"";

        var authHeader = string.Format(headerFormat,
        Uri.EscapeDataString(oauth_callback),
        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_version)
        );

        ServicePointManager.Expect100Continue = false;

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(resource_url);
        request.Headers.Add("Authorization", authHeader);
        //request.Method = "GET";
        request.Method = "POST";
        request.ContentType = "application/x-www-form-urlencoded";
        WebResponse response = request.GetResponse();

I used values for oauth_token_secret , oauth_consumer_key and oauth_consumer_secret taken from application page’s ‘OAuth Tool’ tab.

After running the code, I’m getting response as “Unauthorized”. Is there something I’m missing?