-
Notifications
You must be signed in to change notification settings - Fork 1
/
Program.cs
93 lines (86 loc) · 3.04 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
/*
* KidoZen .NET Client Sample -
*
* Use this sample to test how to consume KidoZen APIs from your
* .NET projects (ie: services, web services, web apis, web sites, etc...
*
* Notice: For Mobile, Xamarin or Windows Phone projects please refer
* to the KidoZen SDKs instead!
*/
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
namespace ClientTest
{
class MainClass
{
public static void Main (string[] args)
{
Run().Wait();
}
/*
* This method will retrieve a KidoZen token using a Service Account
* clientID and clientSecret, and then invoke a KidoZen API (ie: query a datasource).
*
* In order to generate your clientId and clientSecret follow these steps:
* - Go to the application center
* - Admin > Select application > Security > Clients
* - Click in "Create new client" and select "Service Account" from the options.
* - Give your service account Core APIs access (ie: "allow all *")
* - Enable all the data access APIs this client will be using
* - Save it!
*
* Notice: make sure to complete your own "tenant", "app", "clientID" and "clientSecret"
* parameters, as well as double check the API you want to consume.
*/
public static async Task Run()
{
var tenant = "YOUR-TENANT";
var app = "YOUR-APP";
var clientID = "PASTE-CLIENT-ID-HERE";
var clientSecret = "PASTE-CLIENT-SECRET-HERE";
var url = String.Format("https://{0}-{1}.kidocloud.com", app, tenant);
//
// Before we can make an API call, we need a KidoZen token.
//
Console.WriteLine ("Retrieving token");
var authServiceURL = "https://auth.kidozen.com/v1/" + tenant + "/oauth/token";
var client = new HttpClient ();
var tokenResponse = await client.PostAsJsonAsync (authServiceURL, new {
client_id = clientID,
client_secret = clientSecret,
scope = app,
grant_type = "client_credentials",
});
if (!tokenResponse.IsSuccessStatusCode) {
Console.WriteLine ("Error getting auth token");
Console.WriteLine (tokenResponse.Content.ReadAsStringAsync ().Result);
return;
}
var token = await tokenResponse.Content.ReadAsAsync<Token> ();
Console.WriteLine ("Retrieved auth token:" + token.access_token);
//
// In this case we are querying a data access API (aka DataSource), but
// you can use the same token to access any of the other APIs that KidoZen
// exposes.
//
var dsName = "NAME-OF-YOUR-DATA-SOURCE";
Console.WriteLine ("Querying Data Source: " + dsName);
client.DefaultRequestHeaders.Authorization = AuthenticationHeaderValue.Parse ("Bearer " + token.access_token);
var dsUrl = url + "/api/v2/datasources/" + dsName;
var dsResponse = await client.GetAsync(dsUrl);
Console.WriteLine ("Success: " + dsResponse.IsSuccessStatusCode);
Console.WriteLine (dsResponse.Content.ReadAsStringAsync ().Result);
}
/*
* Token
* DTO class used to deserialize the token returned by the KidoZen authentication
* service.
*/
public class Token
{
public string access_token { get; set; }
}
}
}