Created
March 17, 2016 19:39
-
-
Save vineetchoudhary/0f0675ad3ec7eb3d69e5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Newtonsoft.Json; | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Net.Http; | |
using System.Net.Http.Headers; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace TestAnalyticsAPI | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
string tenantId = "<your tenant ID>"; | |
string clientId = "<your client ID>"; | |
string clientSecret = "<your secret>"; | |
string scope = "https://manage.devcenter.microsoft.com"; | |
// Retrieve an Azure AD access token | |
string accessToken = GetClientCredentialAccessToken( | |
tenantId, | |
clientId, | |
clientSecret, | |
scope).Result; | |
// This is your app's product ID. This ID is embedded in the app's listing link | |
// on the App identity page of the Dev Center dashboard. | |
string appID = "<your app's product ID>"; | |
DateTime startDate = DateTime.Parse("08-01-2015"); | |
DateTime endDate = DateTime.Parse("11-01-2015"); | |
int pageSize = 1000; | |
int startPageIndex = 0; | |
// Call the Windows Store analytics API | |
CallAnalyticsAPI(accessToken, appID, startDate, endDate, pageSize, startPageIndex); | |
Console.Read(); | |
} | |
private static void CallAnalyticsAPI(string accessToken, string appID, DateTime startDate, DateTime endDate, int top, int skip) | |
{ | |
string requestURI; | |
// Get app acquisitions | |
requestURI = string.Format( | |
"https://manage.devcenter.microsoft.com/v1.0/my/analytics/appacquisitions?applicationId={0}&startDate={1}&endDate={2}&top={3}&skip={4}", | |
appID, startDate, endDate, top, skip); | |
//// Get IAP acquisitions | |
//requestURI = string.Format( | |
// "https://manage.devcenter.microsoft.com/v1.0/my/analytics/inappacquisitions?applicationId={0}&startDate={1}&endDate={2}&top={3}&skip={4}", | |
// appID, startDate, endDate, top, skip); | |
//// Get app failures | |
//requestURI = string.Format( | |
// "https://manage.devcenter.microsoft.com/v1.0/my/analytics/failurehits?applicationId={0}&startDate={1}&endDate={2}&top={3}&skip={4}", | |
// appID, startDate, endDate, top, skip); | |
//// Get app ratings | |
//requestURI = string.Format( | |
// "https://manage.devcenter.microsoft.com/v1.0/my/analytics/ratings?applicationId={0}&startDate={1}&endDate={2}&top={3}&skip={4}", | |
// appID, startDate, endDate, top, skip); | |
//// Get app reviews | |
//requestURI = string.Format( | |
// "https://manage.devcenter.microsoft.com/v1.0/my/analytics/reviews?applicationId={0}&startDate={1}&endDate={2}&top={3}&skip={4}", | |
// appID, startDate, endDate, top, skip); | |
HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Get, requestURI); | |
requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken); | |
WebRequestHandler handler = new WebRequestHandler(); | |
HttpClient httpClient = new HttpClient(handler); | |
HttpResponseMessage response = httpClient.SendAsync(requestMessage).Result; | |
Console.WriteLine(response); | |
Console.WriteLine(response.Content.ReadAsStringAsync().Result); | |
response.Dispose(); | |
} | |
public static async Task<string> GetClientCredentialAccessToken(string tenantId, string clientId, string clientSecret, string scope) | |
{ | |
string tokenEndpointFormat = "https://login.microsoftonline.com/{0}/oauth2/token"; | |
string tokenEndpoint = string.Format(tokenEndpointFormat, tenantId); | |
dynamic result; | |
using (HttpClient client = new HttpClient()) | |
{ | |
string tokenUrl = tokenEndpoint; | |
using ( | |
HttpRequestMessage request = new HttpRequestMessage( | |
HttpMethod.Post, | |
tokenUrl)) | |
{ | |
string content = | |
string.Format( | |
"grant_type=client_credentials&client_id={0}&client_secret={1}&resource={2}", | |
clientId, | |
clientSecret, | |
scope); | |
request.Content = new StringContent(content, Encoding.UTF8, "application/x-www-form-urlencoded"); | |
using (HttpResponseMessage response = await client.SendAsync(request)) | |
{ | |
string responseContent = await response.Content.ReadAsStringAsync(); | |
result = JsonConvert.DeserializeObject(responseContent); | |
} | |
} | |
} | |
return result.access_token; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment