【发布时间】:2014-06-03 14:57:06
【问题描述】:
以下是我在 Windows Phone 8.1 中使用 REST API 将视频上传到 youtube 的代码。我遵循了here 的指南。它返回我 400 Bad request 和错误 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"
}
}
【问题讨论】:
标签: rest windows-runtime youtube-api windows-phone-8.1 dotnet-httpclient