【问题标题】:400 Bad request while uploading video to youtube using REST API in Windows Phone 8.1在 Windows Phone 8.1 中使用 REST API 将视频上传到 youtube 时出现 400 错误请求
【发布时间】: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


    【解决方案1】:

    YouTube API 文档在示例 JSON 请求中存在错误。 categoryId 值将是 string 而不是 int 和 embeddable 键具有 True 作为值。应该是true,没有大写T。

    也请查看:YouTube API Video Upload Parse Error in C#

    【讨论】:

      猜你喜欢
      • 2011-09-11
      • 2012-06-07
      • 1970-01-01
      • 2015-12-25
      • 1970-01-01
      • 2014-09-04
      • 2020-10-31
      • 2014-11-17
      • 2017-10-03
      相关资源
      最近更新 更多