Using Azure Mobile Services as an API proxy
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.
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/
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.
Now, in Azure portal at https://manage.windowsazure.com, create a new mobile service
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.
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://
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: { consumerkey: "
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.
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://
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,