Created
March 25, 2016 02:39
-
-
Save tobek/9afa4b4c42eb5c11f36a to your computer and use it in GitHub Desktop.
YouTube API v3 upload video and set snippet etc.
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
<!-- More info at http://stackoverflow.com/questions/25595930/youtube-api-v3-over-http-post-cant-set-snippet-while-uploading-a-video-title/ --> | |
<form id="upload-yt-video" action="https://www.googleapis.com/upload/youtube/v3/videos?part=snippet&access_token=YOUR_TOKEN_HERE" method="post" enctype="multipart/form-data"> | |
<input type="text" name="title" placeholder="Video title"> | |
<textarea name="description" placeholder="Video description"></textarea> | |
<input type="file" accept="video/*" name="videoFile"> | |
<input type="submit" value="Upload Video"> | |
</form> | |
<!-- load jQuery --> | |
<!-- load jQuery form plugin https://github.com/malsup/form --> | |
<script> | |
var YOUTUBE_API_TOKEN = 'YOUR_TOKEN_HERE'; | |
var YOUTUBE_VID_CATEGORY_ID = 24; // "entertainment" as an example | |
var YOUTUBE_VID_PRIVACY_STATUS = 'unlisted'; | |
$('#upload-yt-video').ajaxForm({ | |
success: function(res, status) { | |
if (status !== 'success' || ! res.id) { | |
console.error('problem uploading the video, response status:', status, 'response:', res); | |
return; | |
} | |
console.log('form submission successful, video uploaded to youtube! response:', res); | |
updateYouTubeVideo({ | |
id: res.id, | |
token: YOUTUBE_API_TOKEN, | |
title: $('#upload-yt-video [name=title]').val(), | |
description: $('#upload-yt-video [name=description]').val() | |
}, function(err, res) { | |
if (err) { | |
console.error('problem uploading the video - error while updating video attributes after upload:', err); | |
} | |
else { | |
console.log('video updated! upload complete. response:', res); | |
} | |
}); | |
}, | |
error: function() { | |
console.error('form submission failed', arguments); | |
} | |
}); | |
function updateYouTubeVideo(args, cb) { | |
if (!args || !args.id || !args.token) { | |
console.error('missing args: must at least include id and token'); | |
return; | |
} | |
var apiArgs = { | |
id: args.id, | |
snippet: { | |
description: args.description || '', | |
title: args.title || 'Video ' + new Date().toDateString(), | |
categoryId: YOUTUBE_VID_CATEGORY_ID | |
}, | |
status: { | |
privacyStatus: YOUTUBE_VID_PRIVACY_STATUS | |
} | |
}; | |
$.ajax({ | |
type: 'PUT', | |
url: 'https://www.googleapis.com/youtube/v3/videos?part=snippet,status', | |
contentType: 'application/json', | |
headers: { | |
Authorization: 'Bearer ' + args.token | |
}, | |
data: JSON.stringify(apiArgs), | |
dataType: 'text', | |
success: function(data) { | |
if ($.type(data) === "string") data = JSON.parse(data); | |
cb(null, data); | |
}, | |
error: function(request, err){ | |
cb(err); | |
} | |
}); | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment