Skip to content

Instantly share code, notes, and snippets.

@ChrisRisner
Last active December 21, 2015 22:19
Show Gist options
  • Save ChrisRisner/6374682 to your computer and use it in GitHub Desktop.
Save ChrisRisner/6374682 to your computer and use it in GitHub Desktop.
Glass with Mobile Services
exports.get = function(request, response) {
if (request.query && request.query.code) {
//Do stuff
} else {
var redirectUrl = 'https://accounts.google.com/o/oauth2/auth?response_type=code&' +
'redirect_uri=http%3A%2F%2Fglasstest.azure-mobile.net%2Fapi%2Fglassauth&' +
'client_id=<Client ID>&' +
'scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fglass.timeline+' +
'https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fglass.location+' +
'https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.profile&' +
'access_type=offline&approval_prompt=force';
response.send(statusCodes.OK,
'<html><body><script type="text/javascript">(function() {window.location = "'+redirectUrl+'";})();</script>OK!</body></html>'
);
}
};
exports.get = function(request, response) {
var originalResponse = response;
if (request.query && request.query.code) {
var req = require('request');
req.post({
url: 'https://accounts.google.com/o/oauth2/token',
form:{ code: request.query.code,
grant_type: 'authorization_code',
redirect_uri: 'http://glasstest.azure-mobile.net/api/glassauth',
client_id: '<Client ID>',
client_secret: '<Client Secret>'
}
}
, function(error, response, body) {
if (error) {
response.send(statusCodes.OK,
"<html><body>An error occurred posting to oauth2: " + error);
return;
}
var tokenBody = JSON.parse(body);
req.get({
url: 'https://www.googleapis.com/oauth2/v1/tokeninfo?id_token=' + tokenBody.id_token
}, function(tokenInfoError, tokenInfoResponse, tokekInfoBody) {
if (tokenInfoError) {
originalResponse.send(statusCodes.OK,
"<html><body>An error occurred posting to oauth2-tokeninfo: " + tokenInfoError);
return;
}
var claims = JSON.parse(tokekInfoBody);
var accounts = request.service.tables.getTable('accounts');
accounts.where({ user_id : claims.user_id }).read({
success: function(results) {
if (results.length === 0) {
//insert
var newAccount = { user_id : claims.user_id,
access_token : tokenBody.access_token,
refresh_token : tokenBody.refresh_token,
update_date : new Date()
};
accounts.insert(newAccount);
} else {
//update
var account = results[0];
account.access_token = tokenBody.access_token;
account.refresh_token = tokenBody.refresh_token;
account.update_date = new Date();
accounts.update(account);
}
originalResponse.send(statusCodes.OK, "<html><body>Thanks for granting permissions! We'll push to your glass soon!<br /><br /><a href='https://glasstest.azure-mobile.net/api/glassauth'>Click to auth again</a></body></html>");
}
});
});
}
);
}
else {
var redirectUrl = 'https://accounts.google.com/o/oauth2/auth?response_type=code&redirect_uri=http%3A%2F%2Fglasstest.azure-mobile.net%2Fapi%2Fglassauth&client_id=<Client ID>&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fglass.timeline+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fglass.location+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.profile&access_type=offline&approval_prompt=force';
response.send(statusCodes.OK,
'<html><body><script type="text/javascript">(function() {window.location = "'+redirectUrl+'";})();</script>OK!</body></html>'
);
}
};
exports.get = function(request, response) {
var redirectUrl = 'https://accounts.google.com/o/oauth2/auth?response_type=code&' +
'redirect_uri=http%3A%2F%2Fglasstest.azure-mobile.net%2Fapi%2Fglassauth&' +
'client_id=<Client ID>&' +
'scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fglass.timeline+' +
'https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fglass.location+' +
'https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.profile&' +
'access_type=offline&approval_prompt=force';
response.send(statusCodes.OK,
'<html><body><script type="text/javascript">(function() {window.location = "'+redirectUrl+
'";})();</script>OK!</body></html>'
);
};
var req = require('request');
req.post({
url: 'https://accounts.google.com/o/oauth2/token',
form:{ code: request.query.code,
grant_type: 'authorization_code',
redirect_uri: 'http://glasstest.azure-mobile.net/api/glassauth',
client_id: '<Client ID>',
client_secret: '<Client Secret>'
}
}
exports.get = function(request, response) {
var originalResponse = response;
var req = require('request');
var accounts = request.service.tables.getTable('accounts');
accounts.read({
success: function(results) {
results.forEach(function(account) {
req.post({
url: 'https://www.googleapis.com/mirror/v1/timeline?access_token='+ account.access_token,
headers: {'content-type' : 'application/json'},
body: '{"text" : "Check this stuff out!", "notification" : { "notification-level" : "DEFAULT"},'
+'"menuItems": [{ "action" : "DELETE"}]}'
}
, function(error, response, body) {
if (error) {
console.error(error);
originalResponse.send(statusCodes.OK,'<html><body>There was an error posting to glass: ' + error + '</body></html>');
return;
}
originalResponse.send(statusCodes.OK,'<html><body>Posted to glass!</body></html>');
console.log('body:', body);
}
);
});
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment