Last active
December 20, 2015 14:59
-
-
Save emil10001/6151154 to your computer and use it in GitHub Desktop.
Code for this blog post: http://www.recursiverobot.com/post/57348836217/getting-started-with-the-mirror-api-using-node-js
This file contains hidden or 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 googleapis = require('googleapis') | |
, OAuth2Client = googleapis.OAuth2Client | |
, config = require('./config.json'); | |
var oauth2Client = new OAuth2Client(config.CLIENT_ID, config.CLIENT_SECRET, config.REDIRECT_URL); |
This file contains hidden or 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
{ | |
"CLIENT_ID" : "YOUR CLIENT ID", | |
"CLIENT_SECRET" : "YOUR CLIENT SECRET", | |
"REDIRECT_URL" : "http://example.com/oauth2callback" | |
} |
This file contains hidden or 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 success = function(data) { console.log('success',data); } | |
var failure = function(data) { console.log('failure',data); } |
This file contains hidden or 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 gotToken = function () { | |
googleapis | |
.discover('mirror', 'v1') | |
.execute(function(err, client) { | |
if (!!err){ | |
failure(); | |
return; | |
} | |
listTimeline(client, failure, success); | |
insertHello(client, failure, success); | |
}); | |
}; |
This file contains hidden or 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 grabToken = function (code, errorCallback, successCallback){ | |
oauth2Client.getToken(code, function(err, tokens){ | |
if (!!err){ | |
errorCallback(err); | |
} else { | |
console.log('tokens',tokens); | |
oauth2Client.credentials = tokens; | |
successCallback(); | |
} | |
}); | |
}; |
This file contains hidden or 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 insertHello = function(client, errorCallback, successCallback){ | |
client | |
.mirror.timeline.insert({"text": "Hello world"}) | |
.withAuthClient(oauth2Client) | |
.execute(function(err, data){ | |
if (!!err) | |
errorCallback(err); | |
else | |
successCallback(data); | |
}); | |
}; |
This file contains hidden or 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 listTimeline = function(client, errorCallback, successCallback){ | |
client | |
.mirror.timeline.list() | |
.withAuthClient(oauth2Client) | |
.execute(function(err, data){ | |
if (!!err) | |
errorCallback(err); | |
else | |
successCallback(data); | |
}); | |
}; |
This file contains hidden or 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
app.get('/', function(req,res){ | |
if (!oauth2Client.credentials){ | |
var url = oauth2Client.generateAuthUrl({ | |
access_type: 'offline', | |
scope: 'https://www.googleapis.com/auth/glass.timeline' | |
}); | |
res.redirect(url); | |
} else { | |
gotToken(); | |
} | |
res.send('something happened'); | |
res.end(); | |
}); | |
app.get('/oauth2callback', function(req, res){ | |
grabToken(req.query.code, failure, function(){ res.redirect('/'); }); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment