次の方法で共有


Creating a REST Twitter Client With WCF

Since the TwitterSync Facebook app is broken, I wanted to have a client that updates both Twitter and Facebook statuses.  This post shows how I used WCF to update statuses on Twitter.

The first thing to do is to create the contract.

     [ServiceContract]
    public interface ITwitterStatus
    {
        [OperationContract]
        [WebInvoke(UriTemplate = "/statuses/update.xml?status={text}")]
        void UpdateStatus(string text);
    }

We specify an interface as a WCF service, and expose the UpdateStatus as a part of our distributed communication contract using the OperationContract attribute.  We next apply the WebInvoke attribute because we want to use REST style programming, specifying a UriTemplate.  There's no RequestFormat or ResponseFormat needed here since we are not sending a payload, but if we were using a payload we would specify the RequestFormat.Xml value.

The next thing is to create the client implementation.

     using (WebChannelFactory<ITwitterStatus> cf = new WebChannelFactory<ITwitterStatus>("TwitterClient"))
    {                    
        cf.Credentials.UserName.UserName = "your_twitter_username";
        cf.Credentials.UserName.Password = "your_twitter_password";
        ITwitterStatus s = cf.CreateChannel();
        s.UpdateStatus("This is my new Twitter status!  w00t!");
    }

We use the WebChannelFactory type from .NET 3.5 to create the channel and specify our contract from above.  Twitter uses HTTP Basic authentication, and the way to specify that is by using the Credentials.UserName type (although we need one bit of configuration to make this work in the next step).  Finally, we create the channel and call our method.

The last bit is the configuration. 

     <system.serviceModel>      
      <bindings>
        <webHttpBinding>
          <binding name="TwitterConfig">
            <security mode="TransportCredentialOnly">
              <transport clientCredentialType="Basic" realm="Twitter API" />
            </security>
          </binding>
        </webHttpBinding>
      </bindings>
      <client>
        <endpoint 
          address="https://twitter.com"           
          binding="webHttpBinding"
          bindingConfiguration="TwitterConfig" 
          contract="ITwitterStatus"
          name="TwitterClient" />
      </client>
    </system.serviceModel>

We are using the WebHttpBinding type with Basic HTTP authentication.  By default, the WebHttpBinding uses anonymous authentication, so we need to configure it to use Basic HTTP authentication.  We do this by setting the security mode to TransportCredentialOnly, and configuring the transport to use Basic as the authentication scheme.

That's it, it is actually pretty simple once you get the authentication bit correct.  If you find this code useful, make sure you send me a tweet (https://twitter.com/kaevans) and let me know!

Comments

  • Anonymous
    July 31, 2008
    Check out Kirk Allen Evans posting about how to create a Twitter client using its RESTful API and WCF.