Sample Music Client Using Windows Runtime

The following code depicts a simple, end-to-end scenario in C# for Windows Runtime, for obtaining an access token for the Xbox Music Service and then querying the service for information about an artist. In this example, no refresh token is obtained.

  using System;
  using System.Net;
  using System.Text.RegularExpressions;
  using System.Collections.Generic;
  using System.Diagnostics;
  using Windows.Web.Http;

  namespace RestTest
      public class Test
          public static async void Go()
              var client = new HttpClient();

              // Define the data needed to request an authorization token.
              var service = "";
              var clientId = "myClient";
              var clientSecret = "REDACTED";
              var scope = "";
              var grantType = "client_credentials";

              // Create the request data.
              var requestData = new Dictionary<string, string>();
              requestData["client_id"] = clientId;
              requestData["client_secret"] = clientSecret;
              requestData["scope"] = scope;
              requestData["grant_type"] = grantType;

              // Post the request and retrieve the response.
              var response = await client.PostAsync(new Uri(service), new HttpFormUrlEncodedContent(requestData));
              var responseString = await response.Content.ReadAsStringAsync();
              var token = Regex.Match(responseString, ".*\"access_token\":\"(.*?)\".*", RegexOptions.IgnoreCase).Groups[1].Value;

              // Use the token in a new request.
              service = "";
              response = await client.GetAsync(new Uri(service + WebUtility.UrlEncode(token)));
              responseString = await response.Content.ReadAsStringAsync();

Community Additions