var http = require('https');
var tessel = require('tessel');
tessel.syncClock(function () {
var device_id = 'tessel-01';
var password = 'THE TESSEL PASSWORD';
authenticate(device_id, password, function(e,token){
if(e) return console.log("Error:" + e);
getDeviceProfile(token.access_token, function(e, profile){
console.log("Device profile:");
console.log(profile);
});
});
function getDeviceProfile(token, done){
request('{yourDomain}',
'/userinfo',
'GET',
{
"Content-type": "application/json",
"Authorization": "Bearer " + token
},
null,
function(e,response){
if(e) return done(e);
done(null, JSON.parse(response));
});
}
function authenticate(device_id, password, done)
{
request('{yourDomain}',
'/oauth/ro',
'POST',
{
"Content-type": "application/json",
},
JSON.stringify({
client_id: '{yourClientId}',
username: device_id,
password: password,
connection: 'devices',
grant_type: "password",
scope: 'openid'
}),
function(e,response){
if(e) return done(e);
done(null, JSON.parse(response));
});
}
function request(host, path, method, headers, body, done){
var options = {
hostname: host,
path: path,
method: method,
headers: headers
};
var req = http.request(options, function(res) {
res.setEncoding('utf8');
var response = "";
res.on('data', function (chunk) {
response += chunk;
});
res.on('end', function(){
done(null, response);
});
});
req.on('error', function(e) {
done(e);
});
if( body ) req.write(body);
req.end();
}
});