Skip to content

Commit a97ecd3

Browse files
committed
Fixed the missing OAuth token requirement for Twitch Helix API
1 parent 798f430 commit a97ecd3

File tree

3 files changed

+23
-4
lines changed

3 files changed

+23
-4
lines changed

Fritz.StreamTools/Services/TwitchPubSubService.cs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using Microsoft.Extensions.Configuration;
77
using Microsoft.Extensions.DependencyInjection;
88
using Microsoft.Extensions.Hosting;
9+
using Microsoft.Extensions.Logging;
910
using Microsoft.Extensions.Options;
1011
using System;
1112
using System.Collections.Generic;
@@ -27,15 +28,18 @@ public class TwitchPubSubService : IHostedService
2728
private readonly Twitch.PubSub.Proxy _Proxy;
2829
private readonly ConfigurationSettings _Configuration;
2930
private readonly IHostEnvironment _HostEnvironment;
30-
private readonly Dictionary<string, Action<IHubContext<AttentionHub, IAttentionHubClient>>> _ChannelPointActions = new Dictionary<string, Action<IHubContext<AttentionHub, IAttentionHubClient>>>();
31+
private readonly ILogger _Logger;
32+
private readonly Dictionary<string, Action<IHubContext<AttentionHub, IAttentionHubClient>>> _ChannelPointActions = new Dictionary<string, Action<IHubContext<AttentionHub, IAttentionHubClient>>>();
3133

32-
public TwitchPubSubService(IServiceProvider serviceProvider, Twitch.PubSub.Proxy proxy, IHostEnvironment hostEnvironment, IOptions<ConfigurationSettings> settings)
34+
public TwitchPubSubService(IServiceProvider serviceProvider, Twitch.PubSub.Proxy proxy, IHostEnvironment hostEnvironment, IOptions<ConfigurationSettings> settings, ILoggerFactory loggerFactory)
3335
{
3436
_ServiceProvider = serviceProvider;
3537
_Proxy = proxy;
3638
_Configuration = settings.Value;
3739
_HostEnvironment = hostEnvironment;
3840

41+
_Logger = loggerFactory.CreateLogger("TwitchPubSub");
42+
3943
InitializeChannelPointActions();
4044

4145
}
@@ -47,6 +51,12 @@ private void InitializeChannelPointActions()
4751

4852
public Task StartAsync(CancellationToken cancellationToken)
4953
{
54+
55+
if (string.IsNullOrEmpty(_Configuration.PubSubAuthToken)) {
56+
_Logger.LogError("Twitch PubSub token was not provided, unable to start this service");
57+
return Task.CompletedTask;
58+
}
59+
5060
_Proxy.OnChannelPointsRedeemed += _Proxy_OnChannelPointsRedeemed;
5161
_Token = cancellationToken;
5262
_BackgroundTask = _Proxy.StartAsync(new TwitchTopic[] { TwitchTopic.ChannelPoints(_Configuration.UserId) }, _Token);
@@ -59,7 +69,10 @@ private void IdentifyOldManAudio()
5969

6070
var oldManPath = Path.Combine(_HostEnvironment.ContentRootPath, "wwwroot", "contents", "oldman");
6171
var di = new DirectoryInfo(oldManPath);
62-
_OldManSoundFx = di.GetFiles().Select(f => f.Name).OrderBy(x => Guid.NewGuid()).ToArray();
72+
73+
if (di.Exists && di.GetFiles().Any()) {
74+
_OldManSoundFx = di.GetFiles().Select(f => f.Name).OrderBy(x => Guid.NewGuid()).ToArray();
75+
}
6376

6477
}
6578

Fritz.StreamTools/StartupServices/ConfigureServices.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ private static void RegisterGitHubServices(IServiceCollection services, IConfigu
9999
services.AddHttpClient("ShoutoutCommand", c =>
100100
{
101101
c.DefaultRequestHeaders.Add("client-id", configuration["StreamServices:Twitch:ClientId"]);
102+
c.DefaultRequestHeaders.Add("Authorization", $"Bearer {configuration["StreamServices:Twitch:ClientAccessToken"]}");
102103
});
103104

104105
services.AddHostedService<GitHubService>();

Fritz.Twitch/PubSub/Proxy.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ public Proxy(IOptions<ConfigurationSettings> settings, ILoggerFactory loggerFact
4545
public async Task StartAsync(IEnumerable<TwitchTopic> topics, CancellationToken token)
4646
{
4747

48+
if (string.IsNullOrEmpty(_Configuration.PubSubAuthToken)) {
49+
_Logger.LogError("No token configured for Twitch Pubsub");
50+
return;
51+
}
52+
4853
_Topics = topics;
4954
_Reconnect = false;
5055

@@ -173,7 +178,7 @@ private void _PingTimer_Elapsed(object sender, ElapsedEventArgs e)
173178
_PongTimer.Start();
174179
_PingAcknowledged = false;
175180

176-
// TODO: handle the lack of returned PONG message
181+
// TODO: handle the lack of returned PONG message
177182

178183
}
179184

0 commit comments

Comments
 (0)