JSFiddle - React, Tailwind, and code Playground
HTML
<script src="https://cdn.rawgit.com/youtube/api-samples/master/javascript/cors_upload.js"></script>
<span id="signinButton" class="pre-sign-in">
<!-- IMPORTANT: Replace the value of the <code>data-clientid</code>
attribute in the following tag with your project's client ID. -->
<span
class="g-signin"
data-callback="signinCallback"
data-clientid="YOUR_CLIENT_ID_HERE.apps.googleusercontent.com"
data-cookiepolicy="single_host_origin"
data-scope="https://www.googleapis.com/auth/youtube.upload https://www.googleapis.com/auth/youtube">
</span>
</span>
<div class="post-sign-in">
<div>
<img id="channel-thumbnail">
<span id="channel-name"></span>
</div>
<div>
<label for="title">Title:</label>
<input id="title" type="text" value="Default Title">
</div>
<div>
<label for="description">Description:</label>
<textarea id="description">Default description</textarea>
</div>
<div>
<label for="privacy-status">Privacy Status:</label>
<select id="privacy-status">
<option>public</option>
<option>unlisted</option>
<option>private</option>
</select>
</div>
<div>
<input input type="file" id="file" class="button" accept="video/*">
<button id="button">Upload Video</button>
<div class="during-upload">
<p><span id="percent-transferred"></span>% done (<span id="bytes-transferred"></span>/<span id="total-bytes"></span> bytes)</p>
<progress id="upload-progress" max="1" value="0"></progress>
</div>
<div class="post-upload">
<p>Uploaded video with id <span id="video-id"></span>. Polling for status...</p>
<ul id="post-upload-status"></ul>
<div id="player"></div>
</div>
<p id="disclaimer">By uploading a video, you certify that you own all rights to the content or that you are...
CSS
#channel-image {
width: 2em;
height: 2em;
vertical-align: middle;
}
#channel-name {
margin-left: 0.2em;
margin-right: 0.2em;
}
#disclaimer {
font-size: 0.75em;
color: #aeaeae;
max-width: 350px;
}
body {
font-family: "Open Sans", sans-serif;
font-size: 1.5em;
}
.post-sign-in {
display: none;
}
.during-upload {
display: none;
}
.post-upload {
display: none;
}
label {
display: block;
}
input[type="text"], textarea, progress {
font-size: 0.75em;
width: 15em;
margin-bottom: 1em;
padding: 0.5em;
font-family: "Open Sans", sans-serif;
}
textarea {
height: 7em;
}
JavaScript
/*
Copyright 2015 Google Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
var signinCallback = function (result){
if(result.access_token) {
var uploadVideo = new UploadVideo();
uploadVideo.ready(result.access_token);
}
};
var STATUS_POLLING_INTERVAL_MILLIS = 60 * 1000; // One minute.
/**
* YouTube video uploader class
*
* @constructor
*/
var UploadVideo = function() {
/**
* The array of tags for the new YouTube video.
*
* @attribute tags
* @type Array.<string>
* @default ['google-cors-upload']
*/
this.tags = ['youtube-cors-upload'];
/**
* The numeric YouTube
* [category id](https://developers.google.com/apis-explorer/#p/youtube/v3/youtube.videoCategories.list?part=snippet®ionCode=us).
*
* @attribute categoryId
* @type number
* @default 22
*/
this.categoryId = 22;
/**
* The id of the new video.
*
* @attribute videoId
* @type string
* @default ''
*/
this.videoId = '';
this.uploadStartTime = 0;
};
UploadVideo.prototype.ready = function(accessToken) {
this.accessToken = accessToken;
this.gapi = gapi;
this.authenticated = true;
this.gapi.client.request({
path: '/youtube/v3/channels',
params: {
part: 'snippet',
mine: true
},
callback: function(response) {
if (response.error) {
console.log(response.error.message);
} else {
$('#channel-name').text(response.items[0].snippet.title);
...