Category Archives: JSON

Topics containing communications to other languages from JavaScript using JSON objects.

How to consume an OAuth based REST service

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.

HOW TO: Use Facebook Registration In .NET

As we all know Facebook is too big of a monster to not consider when developing our web applications.  Time and time again I have received the request to add a Facebook registration process to a store or blog that allows users to “Connect With Facebook” and remember their settings.  Luckily, Facebook has begun alpha support of a C# library which lets us have some fun with JSON.  Facebook uses a signed response method which encrypts information passed back and forth using your application secret and ID.  Here is some sample code for a page that would call to the Facebook API for registration/logging in.

HTML

<iframe src="https://www.facebook.com/plugins/registration.php?
client_id=xxxx&
redirect_uri=http://localhost/fbtest/completed.aspx&
fields=name,first_name,last_name,birthday,gender,location"
scrolling="auto"
frameborder="no"
style="border:none"
allowTransparency="true"
width="100%"
height="330">
</iframe>

When using this approach Facebook will pass you back to the page you specify in the redirect_url with a Form field setup called signed_request.  The nice thing about this is you create a page that accepts this call back and handles the Form field appropriate to login or register a user.  The C# library that Facebook has started working on exposes some methods which allow you to interpret this response and get some data. Here is an example on how to retrieve the response and fetch some fields from it:

VB.NET

Imports Newtonsoft.Json.Linq
'...
Partial Class FacebookRegistrationPageHandler
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        Dim fbaFacebookApp As New Facebook.FacebookApp
        Dim fsrSignedRequest As Facebook.FacebookSignedRequest
        Dim jobJSONObject As JObject

        fbaFacebookApp.AppId = "xxxx"
        fbaFacebookApp.AppSecret = "xxxx"
        fsrSignedRequest = fbaFacebookApp.SignedRequest

        jobJSONObject = JObject.Parse(fsrSignedRequest.Dictionary("registration"))
        Dim strReturnedName = CType(jobJSONObject("name"), Newtonsoft.Json.Linq.JValue).Value

    End Sub
End Class

C#

using Newtonsoft.Json.Linq;
// ...
Partial;
class FacebookRegistrationPageHandler : System.Web.UI.Page {

    protected void Page_Load(object sender, EventArgs e) {
        Facebook.FacebookApp fbaFacebookApp = new Facebook.FacebookApp();
        Facebook.FacebookSignedRequest fsrSignedRequest;
        JObject jobJSONObject;
        fbaFacebookApp.AppId = "xxxx";
        fbaFacebookApp.AppSecret = "xxxx";
        fsrSignedRequest = fbaFacebookApp.SignedRequest;
        jobJSONObject = JObject.Parse(fsrSignedRequest.Dictionary("registration"));
        object strReturnedName = ((Newtonsoft.Json.Linq.JValue)(jobJSONObject["name"])).Value;
    }
}