Xbox Music API

The introduction of the Xbox Music API begins what I hope is the first successful steps of a music platform. All these changes have been almost universally welcome by die hard Zune fans. We have often seen Xbox Music as the perpetual Dauphin to the much beloved ruler of Microsoft music. The API promises the following:

  • Get the details of an album, an artist, or a track if you know its ID.
  • Search for any album, artist, or track details based on a string of keywords—for example, "Madonna hang out".
  • Get the top tracks and newest albums of an artist.
  • Get a deep link that redirects nicely to the Xbox Music applications, whether on the web, on Windows Phone, on Windows 8, on Windows 8.1, or from some other platform.
  • Get album covers.
  • Get artist images.
  • Become an affiliate and start earning revenue from sales you generate on Xbox Music applications.

This is a solid start, looking forward to much more!

API

Developer authentication is an integral piece to API usage, that is, you will need to create a developer account on the Azure Marketplace and then subscribe to the Xbox Music Platform, which is free. Once the app is registered with Azure you are required to post to the Token Service (with the mandatory fields) in order to get an “Access Token”. The token can then be used to call the RESTful API. The access token is secure, OAuth standard compliant, and flexible. It should be noted that the access token expires after 10 minutes.

There are token retrieval code samples for Windows Store Apps and PHP, here is a rough sample for Windows Phone 8 (assuming you have not imported a HttpClient library).

using System.Text.RegularExpressions;
using System.Text;
using System.IO;

public void PostRequest()
{
string url = "https://datamarket.accesscontrol.windows.net/v2/OAuth2-13";

HttpWebRequest request = HttpWebRequest.Create(new Uri(url)) as HttpWebRequest;

request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";

request.BeginGetRequestStream(new AsyncCallback(StartWebRequestCallBack), request);

}

private void StartWebRequestCallBack(IAsyncResult asyncresult)
{
string clientid = "REDACTED";
string secret = "REDACTED";
string scope = "http://music.xboxlive.com";
string granttype = "client_credentials";

HttpWebRequest webrequest = (HttpWebRequest)asyncresult.AsyncState;
Stream postStream = webrequest.EndGetRequestStream(asyncresult);

UTF8Encoding encoding = new UTF8Encoding();
string data = "client_id=" + clientid + "&client_secret=" + secret + "&scope=" +
scope + "&grant_type=" + granttype;

byte[] bytedata = encoding.GetBytes(data);

postStream.Write(bytedata,0,bytedata.Length);
postStream.Close();

webrequest.BeginGetResponse(new AsyncCallback(FinishWebRequest), webrequest);
}

private void FinishWebRequest(IAsyncResult result)
{
HttpWebResponse response = (result.AsyncState as HttpWebRequest).EndGetResponse(result) as HttpWebResponse;

Stream streamresponse = response.GetResponseStream();
StreamReader sr = new StreamReader(streamresponse);

//The data returned will contain the token which will need to be parsed
var stringresponse = sr.ReadToEnd();

streamresponse.Close();
sr.Close();
}