Upload video on Youtube using curl and api v3
Unfortunately, we don't have a specific example of YouTube API v3 uploads from PHP available yet, but my general advice is:
- Use the PHP client library instead of cURL.
- Base your code on this example written for the Drive API. Because the YouTube API v3 shares a common API infrastructure with other Google APIs, examples for doing things like uploading files should be very similar across different services.
- Take a look at the Python example for the specific metadata that needs to be set in a YouTube v3 upload.
In general, there are a lot of things incorrect with your cURL code, and I can't walk through all the steps it would take to fix it, as I think using the PHP client library is a much better option. If you are convinced you want to use cURL then I'll defer to someone else to provide specific guidance.
Upload thumbnail to Youtube API using curl
You'll have to use the URL that's on the spec document:
https://www.googleapis.com/upload/youtube/v3/thumbnails/set
.
In my experience, the sample code page is not one hundred percent reliable (for example, the Videos.insert
API endpoint suffers from the same issue).
You'll have to issue the following curl
call:
curl --request POST -v \
"https://www.googleapis.com/upload/youtube/v3/thumbnails/set\
?videoId=RoZypUhZY04\
&uploadType=media" \
--header 'Authorization: Bearer my_access_token' \
--header 'Content-Type: image/jpeg'\
--data-binary '@/Users/adviner/Projects/Prototypes/VendorAPI/source/YouTube/YouTube-BOS.jpg'
Notice that instead of the two -F
options (form options), the call above uses one --data-binary
option, with its argument starting with @
to indicate that the rest of the argument is a file name.
YouTube API v3 over HTTP POST: can't set snippet while uploading a video (title ends up as unknown)
I was able to get this work in the end only by making 2 API calls - one to upload the video (as a multipart/form-data
POST) and getting the resulting ID, and a second to update the video by that ID (as an application/json
PUT with snippet
in the body).
I started with an HTML form like this:
<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>
That form uploads the video all on its own, and then in JS you can capture the form submission result to get the video ID, then make a second AJAX call in pure JS to update it:
var YOUTUBE_API_TOKEN = 'YOUR_TOKEN_HERE';
var YOUTUBE_VID_CATEGORY_ID = 24; // "entertainment" as an example - note that a category ID is required
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);
}
});
}
Note #1 - this uses this jQuery plugin to capture the initial form submit.
Disclaimer #1 - I adapted this from a codebase I have that's no longer live, so I wasn't able to test it, so there may be minor typos or bugs in here.
Disclaimer #2 - This worked in September 2014 - there may have been updates to the API since then!
Related Topics
Best Way to Do a Weighted Search Over Multiple Fields in MySQL
Upload Multiple Images with Jquery Ajax and Process Them with PHP
How to Use Pagination with Laravel Db::Select Query
Codeigniter Back Button After Logout
Get Return Value from SQL Stored Procedure Using PHP
Lsa - Latent Semantic Analysis - How to Code It in PHP
Laravel 5.1 Unknown Database Type Enum Requested
MySQL Transactions Within Transactions
Zip All Files in Directory and Download Generated .Zip
Test PHP's Mail Function from Localhost
Preserve and Display Text Exactly How It Is Typed and Submitted
Oracle 11G Xe Greek Character Set Not Displaying
PHP Sessions to Authenticate User on Login Form
Setting Up a Ubuntu/Apache/PHP MAChine to Send Email
A Script to Change All Tables and Fields to the Utf-8-Bin Collation in MySQL