Reputation: 13367
The issue is:
{
"error":{
"errors":[
{
"domain":"youtube.video",
"reason":"mediaBodyRequired",
"message":"Bad Request",
"locationType":"other",
"location":"body"
}
],
"code":400,
"message":"Bad Request"
}
}
My code looks like this:
private void CreateUploadRequest(SynchronisedAsset asset)
{
var endPoint = api.ApiUrl + "/videos?uploadType=resumable&part=snippet"; // read for the different ways to interact with videos https://developers.google.com/youtube/v3/docs/#Videos
var maxSize = 68719476736; // 64 gig
try
{
var location = CompanyProvider.GetUploadLocation(this.baseUploadDirectory, companyId, FileType.Asset);
var filePath = System.IO.Path.Combine(location, asset.FileName);
var fileBytes = System.IO.File.ReadAllBytes(filePath);
if (maxSize > fileBytes.Length && (asset.MimeType.ToLower().StartsWith("video/") || asset.MimeType.ToLower().Equals("application/octet-stream")))
{
var json = Encoding.ASCII.GetBytes("{ \"snippet\": { \"title\": \"" + asset.FileName + "\", \"description\": \"This is a description of my video\" } }");
var request = WebRequest.Create(endPoint);
request.Headers[HttpRequestHeader.Authorization] = string.Format("Bearer {0}", api.Tokens.AccessToken);
request.ContentLength = json.Length;
request.ContentType = "application/json; charset=UTF-8";
request.Headers["X-Upload-Content-Length"] = fileBytes.Length.ToString();
request.Headers["X-Upload-Content-Type"] = asset.MimeType;
request.Method = "POST";
using (var stream = request.GetRequestStream())
{
stream.Write(json, 0, (int)json.Length);
}
var response = request.GetResponse();
}
}
catch (WebException ex)
{
eventLog.WriteEntry("Error uploading to youtube.\nEndpoint: " + endPoint + "\n" + ex.ToString(), EventLogEntryType.Error);
}
}
I am not really sure why it coming up with that error, from the documentation here:
https://developers.google.com/youtube/v3/guides/using_resumable_upload_protocol
it should come back with a 200 ok message, but I don't. According to google api reference the problem is:
The request does not include the video content.
Now I assume that means the actual video because I have already passed the json, but I am unsure how (or even if) you can post the video at the same time as the json.
Please help me, this is driving me nuts :)
Cheers
cheers, /r3plica
Upvotes: 0
Views: 1630
Reputation: 56074
You're leaving a few things out of your example (such as what api.ApiUrl
is set to), but you're making a mistake if you're just trying to send a PUT request to https://www.googleapis.com/youtube/v3/videos
whose body consists entirely of the video bytes.
If you want to implement your own upload code rather than using the client library, I'd strongly suggest that you follow the steps outlined in this HTTP protocol-level guide to resumable uploads. It lists the correct URLs that you should be sending your requests to, as well as the relevant headers and request bodies to use.
The alternatively would be to construct a multipart MIME request body and send that to the correct URL, but resumable uploads is more robust and probably easier to implement by hand. (And I'm not aware of any protocol-level guide to multipart MIME uploads.)
Upvotes: 1
Reputation: 1598
Not sure but in your If statement you have an or statement there perhaps that needs to be surrounded by parentheses , (size && mime ) || octet or size && (mime || octet). I think logic is failing there somehow. As it stands now I bet octets that are over the size limit are what causes your problems. The way your logic reads is if size and mime are ok then go OR if octet stream ok , and your size check never happens. I imagine you want this for your if statement.
if (maxSize > asset.FileSize && (asset.MimeType.ToLower().StartsWith("video/") || asset.MimeType.ToLower().Equals("application/octet-stream")))
Upvotes: 0