将曲目上传到soundcloud(422)Unprocessable Entity时

时间:2014-03-12 14:25:03

标签: c# asp.net-mvc soundcloud

我想尝试将mp3文件上传到我的soundcloud帐户。我为这份工作编写了这段代码。

                        WebClient client = new WebClient();
                        string postData = "client_id=" + "xxxxx"
                                        + "&client_secret=" + "xxx"
                                        + "&grant_type=password&username=" + "xxx" //your username
                                        + "&password=" + "xxx";//your password :)
                        string soundCloudTokenRes = "https://api.soundcloud.com/oauth2/token";
                        string tokenInfo = client.UploadString(soundCloudTokenRes, postData);
                        tokenInfo = tokenInfo.Remove(0, tokenInfo.IndexOf("token\":\"") + 8);
                        string token = tokenInfo.Remove(tokenInfo.IndexOf("\""));
                        System.Net.ServicePointManager.Expect100Continue = false;
                        var request = WebRequest.Create("https://api.soundcloud.com/tracks") as HttpWebRequest;
                        request.CookieContainer = new CookieContainer();
                        //some default headers
                        request.Accept = "*/*";
                        request.Headers.Add("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.3");
                        request.Headers.Add("Accept-Encoding", "gzip,deflate,sdch");
                        request.Headers.Add("Accept-Language", "en-US,en;q=0.8,ru;q=0.6");
                        //file array
                        var files = new UploadFile[] { new UploadFile(filePath, "@/" + filePath, "application/octet-stream") };
                        //other form data
                        var form = new NameValueCollection();
                        form.Add("track[title]", "biksad");
                        form.Add("track[sharing]", "public");
                        form.Add("oauth_token", token);
                        form.Add("format", "json");

                        form.Add("Filename", fileName);
                        form.Add("Upload", "Submit Query");
                        string lblInfo;
                        try
                        {
                            using (var response = HttpUploadHelper.Upload(request, files, form))
                            {
                                using (var reader = new StreamReader(response.GetResponseStream()))
                                {
                                    lblInfo = reader.ReadToEnd();
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            lblInfo = ex.ToString();
                        }
                    }

当我调试此代码部分时。我在catch块中得到(422)Unprocessable Entity错误。为什么我会收到此错误?怎么能解决这个问题?

1 个答案:

答案 0 :(得分:0)

检查Soundcloud文档:

http://developers.soundcloud.com/docs#errors

422 - “请求看起来不错,但是一个或多个参数看起来有些麻烦。你可能以错误的格式发送数据(例如我们期望字符串的数组)。”