Simple SoundCloud Client with C#.Net

By , May 14, 2012 3:11 pm

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

//Client id form SoundCloud
string ClientId = "...";

//Client secret id from SoundCloud
string ClientSecret = "...";

//Credentials (username & password)
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;

//Authentication
string soundCloudTokenRes = "https://api.soundcloud.com/oauth2/token";
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 = "https://api.soundcloud.com/me.xml";
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>https://api.soundcloud.com/users/1650919</uri>
  <permalink-url>http://soundcloud.com/cansik</permalink-url>
  <avatar-url>https://i1.sndcdn.com/avatars-000014659744-62hym4-large.jpg?7ea2471</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>http://geeklife.ch/dev/</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>

Finally

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

Source Code

2 Responses to “Simple SoundCloud Client with C#.Net”

  1. shane says:

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

Leave a Reply

Panorama Theme by Themocracy