forked from MicrosoftDocs/windows-dev-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
123 lines (100 loc) · 4.94 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
//<AnalyticsApiExample>
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 Store ID. This ID is available on
// the App identity page of the Dev Center dashboard.
string appID = "<your app's Store 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 add-on 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;
}
}
}
//</AnalyticsApiExample>