The remote server returned an error: (403) Forbidden?



When using Twitter APIs to send Direct Message through .NET Web Forms Application it returned to me
The remote server returned an error: (403) Forbidden
Status Code and Description return "Forbidden"
so any help?


That sounds like an authentication issue. What URL are you calling and can you share any example code that reproduces the issue? (please do not post your API keys though).


This is the my Code and when send a Tweet using another function it works Successfully but when using the blew function to send direct message it give me an exception “The remote server returned an error: (401) Unauthorized.”

    public void SendDirectMessage(string ScreenName, string Msg)
    // oauth application keys
    //Using API getting secret in token and token in secret
    var oauth_token = ""; 
    var oauth_token_secret = "";
    var oauth_consumer_key = "";
    var oauth_consumer_secret = "";

    //The Twitter json url to to send direct Message
    string resource_url = "";

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

    // create oauth signature
    var baseFormat = "oauth_consumer_key={0}&oauth_nonce={1}&oauth_signature_method={2}" +
    var baseString = string.Format(baseFormat,

    baseString = string.Concat("GET&", 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}\", " +

    var authHeader = string.Format(headerFormat,

    ServicePointManager.Expect100Continue = false;

    // make the request
    //var postBody = "?text" + Uri.EscapeDataString(Msg);// +"&result_type=mixed&count=4";// +"&count=100";//
    var postBody = "?text" + Uri.EscapeDataString(Msg) + "&screen_name=" + Uri.EscapeDataString(ScreenName);// +"&result_type=mixed&count=4";// +"&count=100";//
    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";

    var response = (HttpWebResponse)request.GetResponse();
    var reader = new StreamReader(response.GetResponseStream());
    var objText = reader.ReadToEnd();



I would recommend against writing custom OAuth signature generation code. It’s notoriously difficult to get correct.

Instead you should check out an established API library like twitter4j.


I have already used a library for .NET called tweetSharp but got this exception
{“A connection attempt failed because the connected party did not properly respond after a period of time,
or established connection failed because connected host has failed to respond x.x.x.x:443”}
System.Exception {System.Net.Sockets.SocketException}
So could you help?