Simple SoundCloud Client with C#.Net

SoundCloud is a great platform to share and consume music from all around the world. There are also a lot of clients for every OS. But something I really miss, is a SDK for the .net Framework. There are some approaches in progress, but their not really stable:

So I decided to write a short tutorial about how to connect to the SoundCloud API with C#.NET and OAuth2. Please notice that this tutorial is just a demo and not a library or SDK you can use.

SoundCloud App Center

First you have to register your app in the SoundCloud Developer area:

In the Developer area you can find all resources you need to create your own app. There are links to the documentation of the API, a console application to expose the API and much more.

For our demo application, we need the Client ID and the Client Secret of our application. They are used to identify our application against the REST API.

Implementing the client

After we created our application in the Developer area, we are now ready to write our .Net Client. I’m going to use a C# Console Application with the .Net Framework 4.0.

First we’re going to initialize and declare some variables:

//WebClient to communicate via http WebClient client = new WebClient(); string ClientId = "..."; string ClientSecret = "..."; string username = "..."; string password = "...";

For the authentication, we need a WebClient to communicate via HTTP with the REST API, the Client Id & Client Secret and some user credentials which will be authenticated.

Now we’re ready to talk to the server and ask for a token ;). This is done via the “token” resource of the API. We just have to provide the Client Id, Client Secret and the user credentials:

//Authentication data string postData = "client_id=" + ClientId + "&client_secret=" + ClientSecret + "&grant_type=password&username=" + username + "&password=" + password; string soundCloudTokenRes = ""; string tokenInfo = client.UploadString(soundCloudTokenRes, postData);

If everything was fine, we should receive a JSON Message form the API which contains our access token and some other informations:

{ "access_token":"1-13176-12296782-ae6781b2c93b5eaf2", "expires_in":21599, "scope":"*", "refresh_token":"165e24beb5b6da28ffe055b758b8b206" }

To get the token, we have to parse it out of the JSON context…this is now really ugly parsing and should be done with serialization in a real project:

//Parse the token tokenInfo = tokenInfo.Remove(0, tokenInfo.IndexOf("token":"") + 8); string token = tokenInfo.Remove(tokenInfo.IndexOf("""));

Next step is to call an API resource and check if we really have access to the API. I’m going to call the “Me” resource which will hand over all data about me (logged in user). I’m calling this command with the “.xml” extension so I’ll get an XML response:

//SoundCloud API Get Request string soundCloudMeRes = ""; string meData = client.DownloadString(soundCloudMeRes + "?oauth_token=" + token);

The result should look like this:

<?xml version="1.0" encoding="UTF-8"?> <user> <id type="integer">1650919</id> <kind>user</kind> <permalink>cansik</permalink> <username>cansik</username> <uri></uri> <permalink-url></permalink-url> <avatar-url></avatar-url> <country>Switzerland</country> <full-name>Florian</full-name> <city>Wohlen</city> <description>Electronic experimental music with a lot of LoFi influence! </description> <discogs-name nil="true"></discogs-name> <myspace-name nil="true"></myspace-name> <website></website> <website-title>Homepage</website-title> <online type="boolean">true</online> <track-count type="integer">29</track-count> <playlist-count type="integer">3</playlist-count> <public-favorites-count type="integer">94</public-favorites-count> <followers-count type="integer">137</followers-count> <followings-count type="integer">268</followings-count> <plan>Solo</plan> <private-tracks-count type="integer">8</private-tracks-count> <private-playlists-count type="integer">2</private-playlists-count> <primary-email-confirmed type="boolean">true</primary-email-confirmed> </user>


Yeah, we did it! We’ve created our first SoundCloud client ;)

Source Code

Join the conversation

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  • shane - 7 years ago

    would it be possible for you to do an example on how to upload a song to a client account?

Pings & Trackbacks