11436 SSO

Integrating Philips Hue and Uber APIs: Here's the Code

asagar
Apr 27, 2016

In a previous post, we discussed our experience at the first Philips Hue Hackathon in Bangalore. We integrated the Philips Hue API with Uber’s API to build a mobile app that seamlessly incorporates both platforms to provide a new user experience. Our app enables you to send your Uber ride status to Hue and track your ride’s progress using different colors from the Hue light bulbs.

In this post, we’ll walk through the code our team wrote.

The Philips Hue lighting kits we worked with are exposed on local networks, so any Philips Hue SDK can be used to talk to Hue lighting systems using a local IP address.

Our "Hueber" app was divided into two parts: a server and a web app. The server was built using Node.js and a simple client that’s a single-page web app that used AngularJS and HTML5. The Philips Hue Node SDK made it easy for us to interact with the lighting system.

We split the work between the four members of our team, with each of us getting a different part of the development: integration with Hue, sign in with the Uber OAuth2 login implementation, integration with the Uber APIs, and construction of the HTML5 app.

Integration with Hue

We used the Node Hue API module to talk to the Philips Hue lights. Connecting to them from Node.js required a few simple lines of code:

var hue = require("node-hue-api"),

HueApi = hue.HueApi,

lightState = hue.lightState;

 

var displayResult = function(result) {

console.log(JSON.stringify(result, null, 2));

};

 

var host = "XXX.XX.X.XXX",

username = "XXXXXXXXXXXXX  ",

api = new HueApi(host, username),

state;

 

// Set light state to 'on' with warm white value of 500 and brightness set to 100%

state = lightState.create().on().white(500, 100);

 

// --------------------------

// Using a callback

api.setLightState(5, state, function(err, lights) {

if (err) throw err;

displayResult(lights);

});

Sign in with the Uber OAuth2 login implementation

We used the Node.js passport and passport-oauth2 modules to implement an OAuth 2.0 authorization flow on the server to obtain an access token for the user and securely access the Uber API on the user’s behalf.

passport.use(new OAuth2Strategy({

  authorizationURL: 'https://login.uber.com/oauth/v2/authorize',

  tokenURL: 'https://login.uber.com/oauth/v2/token',

  clientID: 'XXXX',

  clientSecret: 'XXXXX',

  callbackURL: "http://localhost:3000/auth/uber/callback"

},

function(accessToken, refreshToken, profile, cb) {

  var user = {}

  user.token = accessToken

  user.refreshToken = refreshToken

  return cb(null, user);

}

));

 

app.get('/auth/uber', passport.authenticate('oauth2', {

redirect_uri: 'http://localhost:3000/auth/uber/callback',

scope: 'profile request',

state: '3(#0/!~',

response_type: 'code'

}));

 

app.get('/auth/uber/callback',

passport.authenticate('oauth2', {

  successRedirect : '/profile',

  failureRedirect : '/'

}));

 

Uber APIs: request ride

Requesting an Uber is simple: it just required a POST to /v1/requests.

 

//Request Uber Ride

 

app.post('/hueber/v1/request', function(req,res){

console.log("Token " + req.user.token)

placeRequest(req.user.token, function(err, response){

  updateHueLights(getState("on", 255, 98, 0));

  hueAPI.lights().then(displayResult).done();

  res.json(JSON.parse(response))

})

})

 

var placeRequest = function(token, callback){

var options = {

  url : 'https://sandbox-api.uber.com/v1/requests',

  method: 'POST',

  headers:{

    'Authorization':'Bearer ' + token,

    'Content-Type': 'application/json'

  },

  body: JSON.stringify({

    "product_id": "XXXXX",

    "start_latitude": 37.761492,

    "start_longitude": -122.423941,

    "end_latitude": 37.775393,

    "end_longitude": -122.417546

  })

}

 

request(options, function(err, res, body){

  callback(null, body)

})

}

 

Simple single-page web app using AngularJS

AngularJS helped us to quickly connect our front-end logic with back-end data. The implementation of formController fetches data from back-end server APIs.

 

hueberApp.controller('formController', ['$scope', '$timeout', '$stateParams', 'uberApi',

                                     function($scope, $timeout, $stateParams, uberApi) {

$scope.username = ''

$scope.philipsIpAddress = 'XXXX'

$scope.philipsUserName = 'XXXX'

uberApi.getUserDetails()

  .then(function (response) {

    $scope.username = response.data.first_name + ' ' + response.data.last_name;

    $scope.userphoto = response.data.picture

    console.log(response)

  }, function (error) {

    console.log(error)

  })

 

$scope.bookRide = function() {

  uberApi.bookRide($scope.philipsUserName, $scope.philipsIpAddress)

    .then(function (response) {

      alert('Your ride is on its way..')

      console.log(response)

    }, function (error) {

      console.log(error)

    })

}

 

$scope.sendSos = function() {

  uberApi.sendSos($scope.philipsUserName, $scope.philipsIpAddress)

    .then(function (response) {

      alert('Your Family Alerted....')

      console.log(response)

    }, function (error) {

      console.log(error)

    })

}

 

$scope.finishRide = function() {

  uberApi.finishRide($scope.philipsUserName, $scope.philipsIpAddress)

    .then(function (response) {

      alert('Thank you for choosing Hueber....')

      console.log(response)

    }, function (error) {

      console.log(error)

    })

}

 

$scope.stopLights = function() {

  uberApi.stopLights($scope.philipsUserName, $scope.philipsIpAddress)

    .then(function (response) {

      alert('Your Family has been updated....')

      console.log(response)

    }, function (error) {

      console.log(error)

    })

}

 

$scope.enRoute = function() {

  uberApi.enRoute($scope.philipsUserName, $scope.philipsIpAddress)

    .then(function (response) {

      alert('Your Family has been updated....')

      console.log(response)

    }, function (error) {

      console.log(error)

    })

}

}])

 

At the end of the hackathon, we were able to successfully showcase our Hueber app proof of concept with just over 600 lines of custom code. Coming up, we’ll discuss how to build the same app using an API-first Backend-as-a-Service (BaaS) and the advantages of using Apigee BaaS.

 

Microservices Done Right

Next Steps

 
 

Resources Gallery

News