Recently I decided to dive deep into REST services because of how prominent they are becoming in the industry. Nearly most of my coding time is spent figuring out how to take one piece of software or code and connect it with another piece of software or code to produce the expected result. RESTful web services are a great way to take a generic design such as Json or XML and come up with a standard library to talk to any system. If you aren’t familiar with REST services, they are a simple form of HTTP communication that takes basic HTTP commands: GET, PUSH, PUT and DELETE and translates them into CRUD operations.
What does this all mean? It means that you can call a website through a standard website client and treat it just as if you were hitting a data source. Pretty neat huh? A large chunk of the REST services out on the market now deal in Json with support for XML. Json is a nice language that allows for dynamic object design without having any formal definition. If you want to add a field, you add it. It is very extensible and light weight allowing for quick and scalable data operations.
So how does OAuth play into all of this? OAuth is a system that allows you to connect your application to another application in a secure fashion. To start out, you give your application an ID. This is sent to a service in the form of a request for access, and the user who is using your application grants what access they want to give. Once granted your application gets back an authorization key which it uses to make future calls. The beauty of this system is it puts the security and functionality of the application in the hands of the user and the end service. All you need to worry about is what your authorization key is and how what services you should be calling at a given time.
My goto library for this type of call currently is called RestSharp. This library is a REST service library built out for everything we are doing here. Here is an example of how to create a client and make a call:
Dim auth = New RestClient(SiteURL) Dim rqst As New RestRequest("wc-api/v2/orders", Method.GET) Dim rtn As RestResponse rqst.RequestFormat = DataFormat.Json auth.Authenticator = New HttpBasicAuthenticator(ConsumerKey, ConsumerSecret) rtn = auth.Execute(rqst)
With this call, you are reaching out to a popular WordPress plugin called WooCommerce to get a list of orders and returning back the HTTP response. This response will be in a Json string that you can parse using the popular Newtonsoft library. The request has all of the standard CRUD operations as well as numerous authentication libraries on top of OAuth. For more details information about REST services or OAuth, check out the standards page here on W3C.