Skip to content

Instantly share code, notes, and snippets.

@drewww
Created May 28, 2013 18:48
Show Gist options
  • Save drewww/5665130 to your computer and use it in GitHub Desktop.
Save drewww/5665130 to your computer and use it in GitHub Desktop.
NodeJS/express server that demonstrates how to get hangout urls by creating events on an account that has auto-create hangout set. That account is the account of the person completing the oauth process, not the account that creates the CLIENT_ID/CLIENT_SECRET.
var moment = require('moment');
var googleapis = require('googleapis');
var GoogleToken = require('gapitoken');
var OAuth2Client = googleapis.OAuth2Client;
var express = require('express');
var app = express();
var oauth2Client;
app.get('/', function(req, res){
// these two come from the google api console, after creating
// a web service client (as opposed to a service account)
// these
oauth2Client =
new OAuth2Client("************************.apps.googleusercontent.com", // CLIENT_ID
"************************", // CLIENT_SECRET
"http://localhost:3000/oauth2callback"); // callback url. arbitrary, but set at google console API
var url = oauth2Client.generateAuthUrl({
access_type: 'offline',
scope: 'https://www.googleapis.com/auth/calendar'
});
res.redirect(url);
});
app.get('/oauth2callback', function(req, res) {
console.log(req.query["code"]);
oauth2Client.getToken(req.query["code"], function(err, token) {
if(err) {
console.log("error: " + err);
res.send(500, "Error getting token.");
return;
}
oauth2Client.credentials = token;
// okay, we've got a token now. lets actually issue a request.
googleapis.discover('calendar', 'v3').execute(function(err, client) {
var now = moment().format();
client.calendar
.events
.insert({
calendarId: '****************@group.calendar.google.com', // this comes from the calendar settings page towards the bottom
resource: { // this was 'primary' in the original example, but that didn't work for me
summary: 'hangout', // for some reason.
description: 'hangout',
reminders: {
overrides: {
method: 'popup',
minutes: 0
}
},
start: {
dateTime: now
},
end: {
dateTime: now
},
attendees: [{
email: '[email protected]'
}]
}
})
.withAuthClient(oauth2Client)
.execute(function(err, event) {
console.log(err);
console.log(event);
});
});
res.send(200);
});
});
@drewww
Copy link
Author

drewww commented May 28, 2013

Obviously, this should never be used in anything real because the auth clients leak across requests. Just a quick prototype.

@nrmitchi
Copy link

Note that this doesn't seem to work anymore, as the Request lib doesn't seem to separate the resource object into the body on it's own.

It seems that now you have to pass these as two separate parameters; ie:

.insert({
        "calendarId": "****************@group.calendar.google.com"
    },
    {
        "summary": "hangout",
        "description": "hangout",
        "reminders": {
            "overrides": {
                "method": "popup",
                "minutes": 0
            }
        },
        "start": {
            "dateTime": "now"
        },
        "end": {
            "dateTime": "now"
        },
        "attendees": [
            {
                "email": "[email protected]"
            }
        ]
    }
)

Note that if anyone can prove me wrong on this please do.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment