Getting Started with the Xbox Music API
The following code example depicts a simple, end-to-end scenario in C# for obtaining an access token for the Xbox Music Service and then querying the service for information about an artist.
For the complete source code of the console application, see Sample Music Client Using .NET. For a refactored Windows Runtime version of the same code, see Sample Music Client Using Windows Runtime. For a PHP version, see Sample Music Client Using PHP.
Initialize data for new client to obtain a token.
Note
You must use http, not https in the scope. string service = "https://datamarket.accesscontrol.windows.net/v2/OAuth2-13"; string clientId = "myClient"; string clientSecret = "REDACTED"; string clientSecretEnc = System.Uri.EscapeDataString(clientSecret); string token = null; string tokenEnc = null; HttpWebRequest request = null; HttpWebResponse response = null; // NOTE http. not https here! string scope = "http://music.xboxlive.com"; string scopeEnc = System.Uri.EscapeDataString(scope); string grantType = "client_credentials"; string postData = "client_id=" + clientId + "&client_secret=" + clientSecretEnc + "&scope=" + scopeEnc + "&grant_type=" + grantType;
-
POST your request for the token.
string responseString = SendRequest("POST", service, postData); static string SendRequest(string method, string service, string postData) { string responseString = null; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(service); UTF8Encoding encoding = new UTF8Encoding(); byte[] data = encoding.GetBytes(postData); request.Method = method; request.ContentType = "application/x-www-form-urlencoded"; request.ContentLength = data.Length; using (Stream stream = request.GetRequestStream()) { stream.Write(data, 0, data.Length); } using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { responseString = new StreamReader(response.GetResponseStream()).ReadToEnd(); } return responseString; }
-
The JSON response contains the token. Parse away everything but the token itself with a lightweight regular expression.
token = ExtractTokenFromJson(responseString); static string ExtractTokenFromJson(string json) { string token = null; Match match = Regex.Match(json, ".*\"access_token\":\"(?<token>.*?)\".*", RegexOptions.IgnoreCase); if (match.Success) { token = match.Groups["token"].Value; } return token; }
-
Now query the Music database to get information on the band Daft Punk.
Note
You must use https, not http, in the request URL. The /1/
in the URL specifies the API version.tokenEnc = HttpUtility.UrlEncode(token); request = (HttpWebRequest)WebRequest.Create("https://music.xboxlive.com/1/content/music/search?q=daft+punk&accessToken=Bearer+" + tokenEnc); request.Method = WebRequestMethods.Http.Get; request.Accept = "application/json"; using (response = (HttpWebResponse)request.GetResponse()) { using (var sr = new StreamReader(response.GetResponseStream())) { responseString = sr.ReadToEnd(); } }
-
The JSON response contains the requested info. Dump it to the console.
Console.WriteLine(responseString);