locked
400 Bad request while uploading video to youtube using REST API in Windows Phone 8.1 RRS feed

  • Question

  • Below given is my code for uploading video to youtube using REST API in Windows Phone 8.1. I have followed guide from here. It returns me 400 Bad request with error JSON (which is given after code). Anyone has idea what is wrong in my code?

    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();
            }
        }
    }

    Error JSON

    {
     "error": {
      "errors": [
       {
        "domain": "global",
        "reason": "parseError",
        "message": "Parse Error"
       }
      ],
      "code": 400,
      "message": "Parse Error"
     }
    }


    Wednesday, June 4, 2014 8:40 AM

Answers

  • YouTube API documentation has error in sample JSON request. categoryId value would be string not integer & embeddable key has True as value. It should be true, without capital T.
    • Marked as answer by Xyroid Wednesday, June 4, 2014 9:52 AM
    Wednesday, June 4, 2014 9:52 AM