Connecting to and uploading tracks with Soundcloud API using C# .NET

后端 未结 3 1584
旧巷少年郎
旧巷少年郎 2020-12-30 13:22

I\'m trying to upload an audio track to the Soundcloud.com using C#.NET, but there aren\'t any resources for .NET anywhere. Could someone post a link or an example of how to

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-30 13:45

    Here is a code snippet to upload track via the SoundCloud API =>

            using (HttpClient httpClient = new HttpClient()) {
                httpClient.DefaultRequestHeaders.ConnectionClose = true;
                httpClient.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue("MySoundCloudClient", "1.0"));
                httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("OAuth", "MY_AUTH_TOKEN");
                ByteArrayContent titleContent = new ByteArrayContent(Encoding.UTF8.GetBytes("my title"));
                ByteArrayContent sharingContent = new ByteArrayContent(Encoding.UTF8.GetBytes("private"));
                ByteArrayContent byteArrayContent = new ByteArrayContent(File.ReadAllBytes("MYFILENAME"));
                byteArrayContent.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
                MultipartFormDataContent content = new MultipartFormDataContent();
                content.Add(titleContent, "track[title]");
                content.Add(sharingContent, "track[sharing]");
                content.Add(byteArrayContent, "track[asset_data]", "MYFILENAME");
                HttpResponseMessage message = await httpClient.PostAsync(new Uri("https://api.soundcloud.com/tracks"), content);
    
                if (message.IsSuccessStatusCode) {
                    ...
                }
    

提交回复
热议问题