Permalink
Browse files

complete cartodb oauth/xauth node app

  • Loading branch information...
1 parent 21c8820 commit df4991d8da9130302efef78e5e75e027a0096c27 Simon Tokumine committed Sep 27, 2011
Showing with 115 additions and 5 deletions.
  1. +27 −0 LICENCE
  2. +0 −4 README
  3. +15 −0 README.md
  4. +72 −0 app.js
  5. +1 −1 package.json
View
27 LICENCE
@@ -0,0 +1,27 @@
+Copyright (c) 2011, Vizzuality
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+1. Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+2. Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+3. All advertising materials mentioning features or use of this software
+ must display the following acknowledgement:
+ This product includes software developed by Vizzuality.
+4. Neither the name of Vizzuality nor the
+ names of its contributors may be used to endorse or promote products
+ derived from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS "AS IS" AND ANY
+EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
+DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
View
4 README
@@ -1,4 +0,0 @@
-CartoDB OAuth example for node.js
-==
-
-A quick example app showing how to perform Read/Write updates to your CartoDB over OAuth using node.js
View
@@ -0,0 +1,15 @@
+CartoDB OAuth with node.js
+=================================
+
+Perform simple Read/Write updates to your CartoDB over 2 legged OAuth (XAuth) using Node.js
+
+Uses the popular lib 'node-oauth': https://github.com/ciaranj/node-oauth
+
+to run
+------
+complete all the variables/keys at the top of app.js and then:
+
+```bash
+npm install
+node app.js
+```
View
72 app.js
@@ -0,0 +1,72 @@
+// OAuth supports 3 legged and 2 legged (aka XAuth) authentication.
+// This app demonstrates how to use 2 legged authentication with CartoDB
+
+var cartodb_username = '' // your CartoDB username
+ , cartodb_password = '' // your CartoDB password
+ , cartodb_consumer_key = '' // Your CartoDB API Key ('YOUR KEY')
+ , cartodb_consumer_secret = '' // Your CartoDB API Secret ('YOUR SECRET')
+ , cartodb_private_query = '' // An SQL query to run eg. 'SELECT cartodb_id FROM cables LIMIT 20'
+ , cartodb_request_url = 'https://' + cartodb_username + '.cartodb.com/oauth/request_token'
+ , cartodb_access_url = 'https://' + cartodb_username + '.cartodb.com/oauth/access_token'
+ , cartodb_api_url = 'https://' + cartodb_username + '.cartodb.com/api/v1/sql';
+
+
+var sys = require('sys')
+ , querystring = require('querystring')
+ , OAuth = require('oauth').OAuth
+ , oa = new OAuth(cartodb_request_url, cartodb_access_url, cartodb_consumer_key, cartodb_consumer_secret, "1.0", null, "HMAC-SHA1");
+
+// Request temporary request tokens
+oa.getOAuthRequestToken(function(error, request_key, request_secret, results){
+ if(error) sys.puts('error :' + error);
+ else {
+ // Output consumer and request tokens (for completeness)
+ sys.puts('\n== Consumer Tokens ==');
+ sys.puts('consumer key :' + cartodb_consumer_key);
+ sys.puts('consumer secret :' + cartodb_consumer_secret);
+
+ sys.puts('\n== Request Tokens ==');
+ sys.puts('request key :' + request_key);
+ sys.puts('request secret :' + request_secret);
+
+ // Configure XAuth request
+ var xauth = {x_auth_mode:"client_auth", x_auth_username: cartodb_username, x_auth_password: cartodb_password };
+
+ // Request access key and secret tokens via XAuth
+ // ** NOTE: Do NOT post the request_secret in argument 3 **
+ sys.puts("\nRequesting access tokens via XAuth...");
+ oa.post(cartodb_access_url, request_key, null, xauth, null, function(error, data) {
+ if(error) {
+ sys.puts(require('sys').inspect(error));
+ throw new Error("...XAuth failed. Please check your password and username.");
+ } else {
+ sys.puts("...XAuth successful!");
+
+ // Parse access tokens from returned query string
+ var access_tokens = querystring.parse(data);
+ var access_key = access_tokens['oauth_token'];
+ var access_secret = access_tokens['oauth_token_secret'];
+
+ // Output access tokens
+ sys.puts('\n== Access Tokens ==');
+ sys.puts('access key:' + access_key);
+ sys.puts('access secret :' + access_secret);
+
+ // Do a sample GET query
+ var protected_request = cartodb_api_url + "?q=" + querystring.escape(cartodb_private_query);
+ oa.get(protected_request, access_key, access_secret, function (error, data, response) {
+ sys.puts('\n== CartoDB result for GET "' + cartodb_private_query + '" ==');
+ sys.puts(data + '\n');
+ });
+
+ // Do a sample POST query
+ var protected_request = cartodb_api_url;
+ var body = {q: cartodb_private_query}
+ oa.post(protected_request, access_key, access_secret, body, null, function (error, data, response) {
+ sys.puts('\n== CartoDB result for POST "' + cartodb_private_query + '" ==');
+ sys.puts(data + '\n');
+ });
+ }
+ });
+ }
+});
View
@@ -17,6 +17,6 @@
},
"dependencies": {
"underscore" : "1.1.x",
- "node-oauth": "0.2.0"
+ "oauth": "0.9.5"
}
}

0 comments on commit df4991d

Please sign in to comment.