Error 400 with YouTube API and C#?

本文关键字:and API YouTube with Error | 更新日期: 2023-09-27 18:01:18

我正在尝试将标题更新为视频,并在c#中添加授权:

string postUrl = "https://www.googleapis.com/youtube/v3/videos?part=snippet&access_token=" + this.access_token;
string postData = "id=" + this.videoID +
    "&snippet[title]=" + Uri.EscapeDataString(status.Text) +
    "&snippet[categoryId]=20";
byte[] postByte = Encoding.UTF8.GetBytes(postData);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(postUrl);
request.Method = "PUT";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = postByte.Length;
request.Timeout = 15000;
try
{
    Stream putStream = request.GetRequestStream();
    putStream.Write(postByte, 0, postByte.Length);
    putStream.Close();
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
}
catch (WebException err)
{
    MessageBox.Show(err.Message);
}
以上代码显示了以下错误:

远程服务器返回一个错误:(400)Bad Request.

我做错了什么?

Error 400 with YouTube API and C#?

我明白了。此特定端点需要json响应,而不是x-www-form-urlencoded响应。