Created
August 8, 2017 02:50
-
-
Save bobbigmac/18fbd1395817434b207c54eba86608e1 to your computer and use it in GitHub Desktop.
Google Oauth API Auth and Upload videos to Youtube from Meteor server-side
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
//Can be improved significantly, but a start as I had trouble getting any of the youtube npm modules to play well with Meteor. | |
const fs = require('fs'); | |
const Youtube = require("youtube-api"); | |
const opn = require("opn"); | |
const GoogleTokenProvider = require('refresh-token').GoogleTokenProvider; | |
var baseFolder = '/####/'; | |
//For these, go here: https://console.developers.google.com/apis/credentials | |
//Create an Oauth2 ---WEB APPLICATION--- project and download the json file | |
const CREDENTIALS = { | |
"web":{ | |
"client_id": "###", | |
"project_id": "###", | |
"auth_uri": "https://accounts.google.com/o/oauth2/auth", | |
"token_uri": "https://accounts.google.com/o/oauth2/token", | |
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs", | |
"client_secret": "###", | |
"redirect_uris": ["http://localhost:3000/oauth2callback"], | |
"javascript_origins": ["http://localhost:3000"] | |
} | |
}; | |
// Authenticate | |
// You can access the Youtube resources via OAuth2 only. | |
var auth = { | |
type: "oauth", | |
client_id: CREDENTIALS.web.client_id, | |
client_secret: CREDENTIALS.web.client_secret, | |
redirect_url: CREDENTIALS.web.redirect_uris[0], | |
}; | |
var refreshTokenObj = Settings.findOne({ _id: 'google-refresh-token' }); | |
//You could output this on the first call, then just hard-code it if it's only for server-use. | |
//Access tokens usually expire in an hour | |
//Refresh token 'should' last a year | |
var refreshToken = refreshTokenObj && refreshTokenObj.value; | |
let oauth = false; | |
if(refreshToken) { | |
// console.log('Have a youtube refresh token', refreshToken); | |
auth.refresh_token = refreshToken; | |
oauth = Youtube.authenticate(auth); | |
} else { | |
oauth = Youtube.authenticate(auth); | |
//This will only work if you have a web-browser on your system | |
opn(oauth.generateAuthUrl({ | |
access_type: "offline", | |
scope: ["https://www.googleapis.com/auth/youtube.upload"] | |
})); | |
} | |
//Just bypass meteor router to handle this call | |
WebApp.connectHandlers.use('/oauth2callback', Meteor.bindEnvironment(function(req, res, next) { | |
var code = (req && req.query && req.query.code); | |
if(code) { | |
//console.log('Got oauth code', code); | |
oauth.getToken(code, Meteor.bindEnvironment(function(err, tokens) { | |
//console.log('oauth tokens', tokens); | |
Settings.upsert({ _id: 'google-refresh-token' }, { $set: { value: tokens.refresh_token }}); | |
oauth.setCredentials(tokens); | |
})); | |
} else { | |
// Credentials are probably wrong | |
console.error('Could not get youtube upload oauth code'); | |
} | |
res.end(); | |
return; | |
})); | |
// console.log(oauth); | |
Meteor.methods({ | |
'upload-video': function(pageId) { | |
//Make sure logged into youtube | |
var tokenProvider = new GoogleTokenProvider({ | |
refresh_token: refreshToken, | |
client_id: CREDENTIALS.web.client_id, | |
client_secret: CREDENTIALS.web.client_secret | |
}); | |
tokenProvider.getToken(Meteor.bindEnvironment(function (err, token) { | |
//token will be a valid access token. | |
console.log(err, 'refreshed refresh_token', refreshToken, 'got', token); | |
oauth.setCredentials({ | |
access_token: token, | |
refresh_token: refreshToken | |
}); | |
//Upload video | |
var done = false; | |
var req = Youtube.videos.insert({ | |
resource: { | |
// Video title and description | |
snippet: { | |
title: "Testing YoutTube Upload", | |
description: "Test video upload" | |
}, | |
status: { | |
privacyStatus: "private" | |
} | |
}, | |
part: "snippet,status", | |
media: { | |
body: fs.createReadStream(baseFolder + 'Q8bBYPA2PbGzTJcuR_0.mp4') | |
} | |
}, function(err, data) { | |
done = true; | |
console.log("Done uploading."); | |
}); | |
console.log('started uploading'); | |
var interval = Meteor.setInterval(function () { | |
if(done) { | |
Meteor.clearInterval(interval); | |
} else { | |
console.log(req.req.connection._bytesDispatched + ' bytes uploaded.'); | |
} | |
}, 500); | |
})); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment