Created
October 4, 2011 12:43
-
-
Save yeikos/1261545 to your computer and use it in GitHub Desktop.
NodeJS Tuenti API examples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html><html> | |
<head> | |
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script> | |
<script type="text/javascript" src="https://raw.github.com/douglascrockford/JSON-js/master/json2.js"></script> | |
<script type="text/javascript" src="https://raw.github.com/yeikos/nodejs.tuenti/master/jquery.tuenti.js"></script> | |
<script type="text/javascript"> | |
$(function() { | |
var $result = $('#result'); | |
$.tuenti.login('email', 'password', function(request, response) { | |
// login incorrect | |
if (response.error) { | |
$result.css('color', 'red').text('Login incorrect'); | |
return false; | |
} | |
// login correct | |
$result.css('color', 'green').text('Login correct, loading friends...'); | |
// get friends | |
request('getFriendsData', function(response) { | |
// reset output | |
$('<h3></h3>').text('My friends').appendTo( | |
$result.empty().css('color', 'black') | |
); | |
var $ul = $('<ul></ul>').appendTo($result); | |
// show friends | |
$.each(response[0], function(index, item) { | |
$('<li></li>').html(item.name).appendTo($ul); | |
}); | |
}); | |
}); | |
}); | |
</script> | |
</head> | |
<body> | |
<div id="result"></div> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Tuenti = require('./../tuenti.js'), | |
Http = require('http'), | |
Url = require('url'), | |
Qs = require('querystring'), | |
Fs = require('fs'); | |
Http.createServer(function(request, response) { | |
var uri = Url.parse(request.url); | |
response.writeHead(200, { 'Content-Type' : 'text/html' }); | |
if (uri.pathname == '/tuenti') { // tuenti request | |
var data = Qs.parse(uri.query); // parse query | |
Tuenti.gateway(data, function(buffer) { | |
// Send tuenti response to local server | |
response.end(JSON.stringify(buffer)); | |
}); | |
} else { // default request | |
// load html | |
response.end(Fs.readFileSync('gateway-method.html', 'utf-8')); | |
} | |
}).listen(8080); | |
console.log('Server running at http://127.0.0.1:8080'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Tuenti = require('./../tuenti.js'); | |
Tuenti.login('email', 'password', function(request, response) { | |
console.log('Login ' + response.error ? 'incorrect' : 'correct'); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Tuenti = require('./../tuenti.js'), | |
tuentiRequest, | |
tuentiSession; | |
Tuenti.login('email', 'password', function(request, response, session) { | |
// check login | |
if (response.error) { | |
console.log('Login incorrect'); | |
return false; | |
} | |
// (1) tuentiRequest: You can save request function into a global var (tuentiRequest) | |
// and you can use it out of this callback | |
tuentiRequest = request; | |
// (2) tuentiSession: Or simply save session into a global var again (tuentiSession) | |
// and send it like argument in request function | |
tuentiSession = session; // session or data.session_id | |
// ready | |
success(); | |
}); | |
function success() { | |
// (1) tuentiRequest | |
tuentiRequest('getUsersData', { ids: [data.user_id] }, function(response) { | |
console.log('My user data', response[0].users); | |
}); | |
// (2) tuentiSession | |
Tuenti.request('getUserAlbums', { session_id: tuentiSession }, function(response) { | |
console.log('My albums', response); | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Tuenti = require('./../tuenti.js'); | |
Tuenti.login('email', 'password', function(request, data) { | |
if (response.error) { | |
console.log('Login incorrect'); | |
return false; | |
} | |
request('getUsersData', { ids: [data.user_id] }, function(response) { | |
console.log(response[0].users); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment