This projects helps you to make HTTP requests to the Uber Rides API.
Installation
Before you begin, you need to register your app in the Uber developer dashboard. Notice that the app gets a client ID, secret, and server token required for authenticating with the API.
After registering your application, you need to install this module in your Node.js project:
npm install node-uber
Initialization
In order to use this module, you have to import it in your application first:
To make HTTP calls, you need to create an authenticated session with the API. User-specific operations require you to use a OAuth 2 bearer token with specific scopes. Jump to the method overview section to identify required scopes for methods. General operations can use a simple server-token authentication.
Step one: Authorize
To obtain an OAuth 2 bearer token, you have to authorize your application with the required scope. Available scopes are: history, history_lite, profile, request, all_trips, and places.
To do so, you are initially required to redirect your user to an authorization URL. You can generate the authorization URL using uber.getAuthorizeUrl. In case you are using Express, your route definition could look as follows:
The URL will lead to a page where your user will be required to login and approve access to his/her Uber account. In case that step was successful, Uber will issue an HTTP 302 redirect to the redirect_uri defined in the Uber developer dashboard. On that redirect, you will receive an authorization code, which is single use and expires in 10 minutes.
Step two: Receive redirect and get an access token
To complete the authorization you now need to receive the callback and convert the given authorization code into an OAuth access token. You can accomplish that using uber.authorizationAsync. This method will retrieve and store the access_token, refresh_token, authorized scopes, and token expiration date within the uber object for consecutive requests.
Using Express, you could achieve that as follows:
app.get('/api/callback', function(request, response) { uber.authorizationAsync({authorization_code: request.query.code}) .spread(function(access_token, refresh_token, authorizedScopes, tokenExpiration) { // store the user id and associated access_token, refresh_token, scopes and token expiration date console.log('New access_token retrieved: ' + access_token); console.log('... token allows access to scopes: ' + authorizedScopes); console.log('... token is valid until: ' + tokenExpiration); console.log('... after token expiration, re-authorize using refresh_token: ' + refresh_token); // redirect the user back to your actual app response.redirect('/web/index.html'); }) .
Nodeback: Looking for nodeback-style methods? Check out the nodeback-readme.
Step three: Make HTTP requests to available resources
Now that you are authenticated, you can issue requests using provided methods.
For instance, to obtain a list of available Uber products for a specific location, you can use uber.products.getAllForLocationAsync.
In case you are using Express, your route definition could look as follows:
app.get('/api/products', function(request, response) { // extract the query from the request URL var query = url.parse(request.url, true).query; // if no query params sent, respond with Bad Request if (!query || !query.lat || !query.lng) { response.sendStatus(400); } else { uber.products.getAllForLocationAsync(query.lat, query.lng) .then(function(res) { response.json(res); }) .error(function(err) { console.error
Optional: Revoke user access (token)
If your users decide to disconnect or revoke access to their Uber accounts, you can use the uber.revokeTokenAsync method. This will invalidate either access_token or refresh_token. Note that per RFC7009, revoke will return success for any string you pass into the function provided the client_id and client_secret are correct. This includes previously revoked tokens and invalid tokens.
uber.revokeTokenAsync('My_access_token');
Method Overview
Nodeback: Looking for nodeback-style methods? Check out the nodeback-readme.
uber.authorizationAsync({ refresh_token: 'REFRESH_TOKEN' }).spread(function(access_token, refresh_token, authorizedScopes, tokenExpiration) { // store the user id and associated access_token, refresh_token, scopes and token expiration date console.log('New access_token retrieved: ' + access_token); console.log('... token allows access to scopes: ' + authorizedScopes); console.log('... token is valid until: ' + tokenExpiration); console.log('... after token expiration, re-authorize using refresh_token: ' + refresh_token);}) .error(function(err) { console.error(err); });});
/products
The product endpoint can be accessed either with an OAuth access_token or simply with the server_token because it is not user-specific. It has, therefore, no required scope for access.
This method utilizes geocoder to retrieve the coordinates for an address using Google as the provider. It uses the first element of the response. In other words, the coordinates represent what the Google algorithm provides with most confidence value.
Note: To ensure correct coordinates you should provide the complete address, including city, ZIP code, state, and country.
uber.products.getAllForAddressAsync(address);
Example
uber.products.getAllForAddressAsync('1455 Market St, San Francisco, CA 94103, US').then(function(res) { console.log(res); }).error(function(err) { console.error(err); });
The estimates endpoint can be accessed either with an OAuth access_token or simply with the server_token because it is not user-specific. It has, therefore, no required scope for access.
This method utilizes geocoder to retrieve the coordinates for an address using Google as the provider. It uses the first element of the response. In other words, the coordinates represent what the Google algorithm provides with most confidence value.
Note: To ensure correct coordinates you should provide the complete address, including city, ZIP code, state, and country.
This method utilizes geocoder to retrieve the coordinates for an address using Google as the provider. It uses the first element of the response. In other words, the coordinates represent what the Google algorithm provides with most confidence value.
Note: To ensure correct coordinates you should provide the complete address, including city, ZIP code, state, and country.
The history endpoint can be accessed ONLY with an OAuth access_token authorized with either the history or history_lite (without city information) scope.
Note: By default, only details about trips your app requested will be returned. This endpoint can be used with the scope all_trips to get all trips irrespective of which application initiated them.
The partners endpoints (Driver API) can be accessed ONLY with an OAuth access_token authorized with the respective scopes (partner.accounts, partner.trips, or partner.payments).
You can execute all existing tests using script test/allTests.js. These tests include linting, code coverage, and unit tests.
npm test
In case you would like to contribute to this project, please ensure that all the tests pass before you create a PR. We have strict code style and code coverage (>= 95%) requirements.