-
Notifications
You must be signed in to change notification settings - Fork 0
/
MatchStatePlatformAuth.cs
40 lines (35 loc) · 1.33 KB
/
MatchStatePlatformAuth.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
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace GeniusSports.AblyMatchStateSubscriberExample
{
class MatchStatePlatformAuth
{
private readonly Options options;
private readonly UrlProvider urlProvider;
public MatchStatePlatformAuth(Options options, UrlProvider urlProvider)
{
this.options = options;
this.urlProvider = urlProvider;
}
/// <summary>
/// Gets access token that allows to access Match State Platform API.
/// </summary>
public async Task<string> GetAccessToken()
{
var body = new Dictionary<string, string> {
{"grant_type", "client_credentials"},
{"client_id", options.ClientId},
{"client_secret", options.ClientSecret}
};
var httpClient = new HttpClient();
var response = await httpClient.PostAsync(urlProvider.AuthUrl, new FormUrlEncodedContent(body));
var responseBody = await response.Content.ReadAsStringAsync();
var tokenEnvelope = JsonConvert.DeserializeObject<JObject>(responseBody);
var accessToken = tokenEnvelope["access_token"].ToString();
return accessToken;
}
}
}