下面给出了我在Windows Phone 8.1中使用REST API将视频上传到youtube的代码。我跟随了here的指南。它返回400 Bad请求,错误JSON(在代码后给出)。任何人都知道我的代码有什么问题吗?
public async Task UploadVideoAsync(Token AuthToken)
{
string json = @"{
""snippet"": {
""title"": ""using API"",
""description"": ""This is a description of my video"",
""tags"": [""cool"", ""video"", ""more keywords""],
""categoryId"": 21,
},
""status"": {
""privacyStatus"": ""public"",
""embeddable"": True,
""license"": ""youtube""
}
}";
var JsonReqMsg = new HttpStringContent(json);
JsonReqMsg.Headers.ContentType = new Windows.Web.Http.Headers.HttpMediaTypeHeaderValue("application/json")
{
CharSet = "UTF-8"
};
ulong ReqSize = 0;
JsonReqMsg.TryComputeLength(out ReqSize);
JsonReqMsg.Headers.ContentLength = ReqSize;
var videoFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/test.mp4"));
var prop = await videoFile.GetBasicPropertiesAsync();
var request = new HttpRequestMessage(HttpMethod.Post, new Uri("https://www.googleapis.com/upload/youtube/v3/videos?uploadType=resumable&part=snippet,status"));
request.Headers.Add("X-Upload-Content-Length", prop.Size.ToString());
request.Headers.Add("x-upload-content-type", "video/*");
request.Content = JsonReqMsg;
var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Add("Authorization", AuthToken.TokenType + " " + AuthToken.AccessToken);
var UploadReq = await httpClient.SendRequestAsync(request);
if (UploadReq.IsSuccessStatusCode)
{
string _VideoUrl = string.Empty;
var res = await UploadReq.Content.ReadAsStringAsync();
UploadReq.Headers.TryGetValue("Location", out _VideoUrl);
var binaryContent = new HttpBufferContent(await FileIO.ReadBufferAsync(videoFile));
var UploadReq_ = await httpClient.PutAsync(new Uri(_VideoUrl), binaryContent);
if (UploadReq_.IsSuccessStatusCode)
{
var res_ = await UploadReq_.Content.ReadAsStringAsync();
}
}
}
错误JSON
{
"error": {
"errors": [
{
"domain": "global",
"reason": "parseError",
"message": "Parse Error"
}
],
"code": 400,
"message": "Parse Error"
}
}
答案 0 :(得分:0)
YouTube API文档在示例JSON请求中有错误。 categoryId
值为string
而不是int
& embeddable
密钥的值为 True 。它应为true
,没有资本 T 。