Created
May 28, 2013 18:48
-
-
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.
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 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); | |
}); | |
}); |
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
Obviously, this should never be used in anything real because the auth clients leak across requests. Just a quick prototype.