问题是:
{
"error":{
"errors":[
{
"domain":"youtube.video",
"reason":"mediaBodyRequired",
"message":"Bad Request",
"locationType":"other",
"location":"body"
}
],
"code":400,
"message":"Bad Request"
}
}
我的代码如下所示:
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);
}
}
从此处的文档中,我不确定为什么会出现该错误:
https://developers.google.com/youtube/v3/guides/using_resumable_upload_protocol
它应该返回一个 200 ok 消息,但我没有。根据google api reference,问题是:
该请求不包括视频内容。
现在我假设这意味着实际的视频,因为我已经通过了 json,但我不确定你如何(或者即使)你可以在 json 的同时发布视频。
请帮助我,这让我发疯:)
干杯
干杯,/r3plica