How to programmatically get App reviews from Windows Store

"Hello World"!

We just announced the availability of the Windows Store analytics API. With these APIs you can get many useful information directly from the Windows Store, without the need to pick up a browser and login in the Dev Center. More important, you can now programmatically access to these information!

Let's see, for example, how to write a simple method that downloads all the reviews for a specific app.

The API to use is the following:

https://manage.devcenter.microsoft.com/v1.0/my/analytics/reviews?applicationId={applicationID}&startDate={startDate}

The only required parameter is the applicationID, but it supports many optional parameters to narrow down the results and to filter based on the reviewer properties or the app marketplace. Interesting, this API does support also a basic query language.

The API is part of the new Azure Resource Manager namespace, so as every other Azure REST API in this namespace is secured with Azure AD. This means it can be consumed only by authorized users, hence for each call you must provide a JWT token to prove your identity. As per documentation, this token must be set as http header:

Authorization: Bearer XXXXXXXXXYYYYZZZZZZ…==

So the first step is to retrieve the token from Azure AD but in order to be able to get the JWT there are some preliminary steps you need to complete. Basically you have to link your Dev Center account with your Azure AD tenant and create an application that will act as "bridge" to the Windows Store APIs. This application will be used to create the Azure access tokens you need.

Once you have completed the preliminary steps, you can use the following code to get the token:

 private async Task<string> GetDevStoreToken()
{
    string clientID = "XXXXX-ZZZZZZZ-YYYY-OOOOO-UUUUUUUUUU"; //from old Azure portal (application setting page) or from Dev Center (New Key button)
    string directoryName = "yourdirectoryname.com";
    string key = "XXXXXXXXXXXXXXXXXXXXXXX="; //from Dev Center (New Key button)
    var authenticationContext = new AuthenticationContext("https://login.windows.net/" + directoryName);

    var credential = new ClientCredential(clientID, key);
    var result = await authenticationContext.AcquireTokenAsync("https://manage.devcenter.microsoft.com", credential);
    
    if (result == null)
    {
       throw new InvalidOperationException("Failed to obtain the JWT token");
    }
    string token = result.AccessToken;
    return token;
}

The code uses the latest pre-release version of Microsoft.IdentityModel.Clients.ActiveDirectory to trigger the OAuth2 flow and retrieve a JWT token (returned as Base64 string).
Once you have the token you can attach it as HTTP header and call the API, it is really straightforward:

 private async Task GetReviewsAsync()
{
    //This is embedded in the app's listing link that is available on the App identity page of the Dev Center dashboard. 
    string applicationID = "YYYYYYYYYYYYY".ToUpperInvariant(); //Important: must be uppercase
    DateTime startDate = new DateTime(2000, 1, 1);
    Uri reviewAPIUrl = new Uri($"https://manage.devcenter.microsoft.com/v1.0/my/analytics/reviews?applicationId={applicationID}&startDate={startDate.ToShortDateString()}");
    
    WebClient client = new WebClient();
    
    var token = await GetDevStoreToken();
    client.Headers.Add(HttpRequestHeader.Authorization, "Bearer " + token);
    
    var result = client.DownloadString(reviewAPIUrl);
    return result;
}

The API returns a JSON document with all the reviews that match the input filter, a total count and an optional link to the next page (if any). In this example I'm just returning the result to the caller, but you can easily write a method to parse the JSON and build an array of objects that represent your reviews.