· 5 years ago · Feb 11, 2020, 04:46 PM
1public class TwitchAuthService : ITwitchAuthService
2{
3 private const string CLIENT_ID = ".....";
4 private const string CLIENT_SECRET = ".....";
5 private const string ACCESS_TOKEN = "....";
6
7 private readonly string _uri;
8 private readonly string _id;
9
10 private TwitchAPI _api;
11
12 public TwitchAuthService()
13 {
14 _api = new TwitchAPI();
15 _api.Settings.AccessToken = ACCESS_TOKEN;
16 _api.Settings.ClientId = CLIENT_ID;
17 _api.Settings.Secret = CLIENT_SECRET;
18
19 CreatedFlow authFlow = _api.ThirdParty.AuthorizationFlow.CreateFlow("InvChatBotV3", new List<AuthScopes>() {
20 AuthScopes.Viewing_Activity_Read,
21 AuthScopes.User_Read
22 });
23
24 _uri = authFlow.Url;
25 _id = authFlow.Id;
26 }
27
28 public string GetUri()
29 {
30 return _uri;
31 }
32
33 public async Task<Bot> GetAuthorizatedBotAsync()
34 {
35 Bot newAuthorizatedBot = await Task.Run(() =>
36 {
37 PingResponse response = null;
38 while (string.IsNullOrEmpty(response.Username))
39 {
40 response = _api.ThirdParty.AuthorizationFlow.PingStatus(_id);
41 }
42 Bot bot = new Bot()
43 {
44 Username = response.Username,
45 OauthToken = response.Token
46 };
47 return bot;
48 });
49
50 return newAuthorizatedBot;
51 }
52}