Getting Error: NameResolutionFailure While Authenticating Twitter Account Via Oauth


#1

I have written this code to get the oauth_token and oauth_token_secret for my twitter application. But I am getting NameResolutionFailure constantly. Please help. What is the problem with this code?

I have my consumer key and consumer secret key. Now i have done “POST oauth/request_token” to get oauth_token and oauth_token_secret. For signature generation I have used HMAC-SHA1. Here is my code.

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

public class POSTOAUTHREQUEST
{
public static string generateParameterString(string method, string url, SortedDictionary<string, string> sd) {
string parameterString = String.Empty;
parameterString += method + “&”;
parameterString += Uri.EscapeDataString(url)+ “&”;

    foreach (KeyValuePair<string,string> entry in sd)
        parameterString += Uri.EscapeDataString(entry.Key + "=" + entry.Value + "&");

    parameterString = parameterString.Substring(0, parameterString.Length - 3);
    return parameterString;
}

public static string generateSignatureKey (String signingKey, String baseString){
    HMACSHA1 hasher = new HMACSHA1(new ASCIIEncoding().GetBytes(signingKey));

    string signatureString = Convert.ToBase64String(hasher.ComputeHash(new ASCIIEncoding().GetBytes(baseString)));
    return signatureString;    
}

public static void Main(string[] Fileargs)
{
    string oauth_consumer_key = "your consumer key";
    string oauth_nonce = Convert.ToBase64String(new ASCIIEncoding().GetBytes(DateTime.Now.Ticks.ToString()));
    string oauth_signature_method = "HMAC-SHA1";

    TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);   
    string oauth_timestamp = Convert.ToInt64(ts.TotalSeconds).ToString();
    string oauth_version = "1.0";

    SortedDictionary<string, string> sd = new SortedDictionary<string, string>();

    sd.Add("oauth_callback","oob");
    sd.Add("oauth_consumer_key", oauth_consumer_key);
    sd.Add("oauth_version", oauth_version);
    sd.Add("oauth_nonce", oauth_nonce);
    sd.Add("oauth_signature_method", oauth_signature_method);
    sd.Add("oauth_timestamp", oauth_timestamp);

    string baseurl = "https://api.twitter.com/oauth/request_token";
    string baseString = generateParameterString("POST", baseurl, sd);
    Console.WriteLine (baseString);

    string consumerSecret = "your consumer key";
    string oauth_token_secret = "your consumer secret key";

    string signingKey = Uri.EscapeDataString(consumerSecret) + "&" + 
        Uri.EscapeDataString(oauth_token_secret);

    string signatureString = generateSignatureKey (signingKey, baseString);
    Console.WriteLine (signatureString);

    ServicePointManager.Expect100Continue = false;

    string authorizationHeaderParams = String.Empty;
    authorizationHeaderParams += "OAuth ";
    authorizationHeaderParams += "oauth_nonce=" + "\"" + Uri.EscapeDataString(oauth_nonce) + "\",";
    authorizationHeaderParams += "oauth_signature_method=" + "\"" + Uri.EscapeDataString(oauth_signature_method) + "\",";
    authorizationHeaderParams += "oauth_timestamp=" + "\"" + Uri.EscapeDataString(oauth_timestamp) + "\",";
    authorizationHeaderParams += "oauth_consumer_key=" + "\"" + Uri.EscapeDataString(oauth_consumer_key) + "\",";
    authorizationHeaderParams += "oauth_signature=" + "\"" + Uri.EscapeDataString(signatureString) + "\",";
    authorizationHeaderParams += "oauth_version=" + "\"" + Uri.EscapeDataString(oauth_version) + "\"";

    HttpWebRequest hwr = (HttpWebRequest)WebRequest.Create(@"https://api.twitter.com/oauth/request_token?oauth_callback=oob");
    hwr.Method = "POST";
    hwr.Headers.Add("Authorization", authorizationHeaderParams);
    hwr.KeepAlive = true;
    hwr.UserAgent = "Your appname";

    hwr.Timeout = 3 * 60 * 1000;

    try 
    { 
        HttpWebResponse rsp = hwr.GetResponse() 
            as HttpWebResponse;
    }
    catch (WebException e)
    {
        Console.WriteLine(e.Message);
    }
}

}