Search code examples
c#twittertwitterizer

Proxy Authentication for Twitterizer2 in C#


Currently the Code i am using to update twitter status is as follows...

public static void SendMessage(string message)
    {
        try
        {
            tokens = new OAuthTokens();
            tokens.AccessToken = "Some Token";
            tokens.AccessTokenSecret = "Some Secret";
            tokens.ConsumerKey = "Some Key";
            tokens.ConsumerSecret = "Some CSecret";    

           TwitterResponse<TwitterStatus> tweetResponse = TwitterStatus.Update(tokens, message);

        }

Please spoon feed me on how to authenticate myself for a proxy on 10.0.0.21 and port 3128 requesting for a username and password.

I am able to access the net and download webpages from C# application using the following code but am not able to update twitter from twitterizer due to this issue...

            WebClient w = new WebClient();

            WebProxy p = new WebProxy("10.0.0.21", 3128);

            p.Credentials = new NetworkCredential("UserName", "Password");

            w.Proxy = p;

            string s = w.DownloadString(SomeUrl);

How to do the same in twitterizer2 package...? Its not taking from IE because Authentication is required for this proxy.

I am not sure of how to modify the configuration file. One more issue is i will not run this application always behind a proxy..mostly it will be run outside a proxy.

Please help. Thanks.


Solution

  • Twitterizer handles proxy settings in 3 ways. They are, in order that Twitterizer chooses which to use:

    1. Every method has an optional parameters argument. They are all based on a single class that has a proxy property that allows you to provide a web proxy exactly the same way that you did in your example. This is good if you have a small amount of Twitter integration, but obviously not great for larger applications.
    2. You can specify your proxy in your configuration file using the <defaultproxy> element. Good for whole applications, but unfortunately won't handle the use of authentication.
    3. The default IE settings are used.

    Since your proxy requires authentication, the only option is to specify the optional properties class to every method.

    For example:

    public static void SendMessage(string message)
    {
        try
        {
            tokens = new OAuthTokens();
            tokens.AccessToken = "Some Token";
            tokens.AccessTokenSecret = "Some Secret";
            tokens.ConsumerKey = "Some Key";
            tokens.ConsumerSecret = "Some CSecret";    
    
            WebProxy p = new WebProxy("10.0.0.21", 3128);
            p.Credentials = new NetworkCredential("UserName", "Password");
    
            StatusUpdateOptions options = new StatusUpdateOptions();
            options.Proxy = p;
    
           TwitterResponse<TwitterStatus> tweetResponse = TwitterStatus.Update(tokens, message, options);
    
        }
    }