-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEpicEcom.cs
50 lines (43 loc) · 1.96 KB
/
EpicEcom.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http.Headers;
using System.Net.Http.Json;
using System.Text;
using System.Threading.Tasks;
namespace EricLauncher
{
class EpicEcomToken
{
public string? token { get; set; }
}
class EpicEcom
{
public const string API_BASE = "https://ecommerceintegration-public-service-ecomprod02.ol.epicgames.com";
private const string OWT_API_TEMPLATE = "/ecommerceintegration/api/public/platforms/EPIC/identities/{0}/ownershipToken";
private EpicAccount Account;
private HttpClient HTTPClient;
public EpicEcom(EpicAccount account)
{
Account = account;
HTTPClient = new();
HTTPClient.BaseAddress = new Uri(API_BASE);
HTTPClient.DefaultRequestHeaders.Accept.Clear();
HTTPClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HTTPClient.DefaultRequestHeaders.Add("Authorization", "bearer " + account.AccessToken!);
}
public async Task<string?> GetOwnershipToken(string catalog_namespace, string catalog_item_id)
{
string formatted_url = string.Format(OWT_API_TEMPLATE, Account.AccountId!);
HttpResponseMessage resp = await HTTPClient.PostAsync(formatted_url, new StringContent($"nsCatalogItemId={catalog_namespace}:{catalog_item_id}", Encoding.UTF8, "application/x-www-form-urlencoded"));
if (!resp.IsSuccessStatusCode)
{
EpicError? error_response = await resp.Content.ReadFromJsonAsync<EpicError>();
Console.WriteLine($"Failed to fetch ownership token: '{error_response!.errorMessage}' ({error_response!.errorCode})");
return null;
}
EpicEcomToken? ecom_response = await resp.Content.ReadFromJsonAsync<EpicEcomToken>();
return ecom_response!.token;
}
}
}