Export (0) Print

Getting Started with the Xbox Music API

3 out of 3 rated this helpful - Rate this topic

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.

  1. Initialize data for new client to obtain a token.

    Dn546659.note(en-us,IEB.10).gifNote
    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;
    
  2. 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;
      }
    
  3. 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;
      }
    
  4. Now query the Music database to get information on the band Daft Punk.

    Dn546659.note(en-us,IEB.10).gifNote
    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();
          }
      }
    
  5. The JSON response contains the requested info. Dump it to the console.

      Console.WriteLine(responseString);
    

Community Additions

ADD

Problem with find token

I have problem when my application find token inside response, i have after access_token an address like





http%3a%2f%2fschemas.xmlsoap.org%2fws%2f2005.........


Is it correct ?





4/23/2014
Show:
© 2013 Microsoft. All rights reserved.