Quantcast
Channel: Sertac Ozercan
Viewing all articles
Browse latest Browse all 10

Using Azure Mobile Services as an API proxy

$
0
0

API proxies can be used for many applications including logging, analytics, convenience, security, changing backend services without affecting developers and many more.

In this post, we are going to see two examples of API proxies (Twitter and Ordrin), and how to set up using Microsoft Azure Mobile Services.

Twitter

Since Twitter switched to v1.1 API (and retired access to the v1 API), it might be a little tricky to access to Twitter API since it now uses oauth authentication to get any data. Azure Mobile Services makes it very easy to access Twitter (and Facebook, Google and Microsoft Live Connect).

For this example, we are going to be retrieve Twitter v1.1 API data by authenticating Twitter on Azure Mobile Services and setting up an API proxy for any search queries.

First thing is to create a Twitter app at https://apps.twitter.com/

twitterAPI-1

Once the app is registered, it will provide 4 types of keys: API key, API secret, access token and access token secret, which we will use in the Azure mobile service.

twitterAPI-2

Now, in Azure portal at https://manage.windowsazure.com, create a new mobile service

twitterAPI-3

And then create a new API. For GET request, you might want to change it as “Everyone”, so anyone without a key can access it.

twitterAPI-4

In the API section, use this code and replace API keys with your Twitter app. This provides a response to a GET method that includes a single query, and uses Twitter API to authenticate to twitter and search for the given query and respond back with the results. GET request should be to

https://<serviceName>.azure-mobile.net/api/<apiName>?q=<searchQuery>

twitterAPI-5

var http = require('request');
exports.get = function (request, response) {
    var twitterUrl = "https://api.twitter.com/1.1/search/tweets.json?q=%23" + request.query.q;

    // Create a new request with OAuth credentials.
    http.get({
        url: twitterUrl,
        oauth: {
           consumer_key: "<API KEY>",
           consumer_secret: "<API SECRET>",
           token: "<ACCESS TOKEN>",
           token_secret: "<ACCESS SECRET>"
        }},
        function (error, resp, body) {
            if (!error && resp.statusCode == 200) {
                response.send(200, body);
            } else {
                console.error('Could not contact Twitter');
            }
    });
}

Ordrin

Ordrin is an API provider that enables food ordering for any applications. Unfortunately, they don’t support Cross Origin Resource Sharing (CORS) yet, so xhr requests from the web is not accepted by their server, since the request isn’t in the list of Access-Control-Allow-Origin that Ordrin accepts.

To get around this limitation, we can set up an API proxy through Azure Mobile Services to route all request to the API there.

To get started, go to https://hackfood.ordr.in/ and register and create a new app.

Ordrin-1

Ordrin-2

Create a new API just like in the previous example and replace the secret key.

In this API call, we are going to be requesting list of restaurants in the area that delivers ASAP, and that requires a zip, city, and address. GET request should be sent to: (this time we have 3 different queries)

https://<serviceName>.azure-mobile.net/api/<apiName>?zip=<searchQuery>?city=<searchQuery>?address=<searchQuery>

Ordrin-3

var http = require('request');
exports.get = function(request, response) {
        var zip = request.query.zip;
        var city = request.query.city;
        var address = request.query.address;
        var url = 'https://r-test.ordr.in/dl/ASAP/'+zip+'/'+city+'/'+address+'?_auth=1,<YOUR KEY>';

        http.get({
            url: url
        }, function(err, getresponse, body) {
            if (err) {
                request.respond(statusCodes.INTERNAL_SERVER_ERROR, 'Unable to connect.');
            } else if (response.statusCode !== 200) {
                request.respond(statusCodes.BAD_REQUEST, 'Bad request');
            } else {
                request.respond(200, body);
            }
        });
};

Viewing all articles
Browse latest Browse all 10

Latest Images

Trending Articles





Latest Images