internal static void Refresh() { try { // Wait a few seconds then return if the current broadcast is null if (YouTubeLiveBroadcast.currentBroadcast == null) { Thread.Sleep(5000); return; } //Plugin.Log($"Requesting chat messages for live chat with id {YouTubeChannel.liveOrDefaultChatId}..."); HttpWebRequest web = (HttpWebRequest)WebRequest.Create($"https://www.googleapis.com/youtube/v3/liveChat/messages?liveChatId={YouTubeLiveBroadcast.currentBroadcast.snippet.liveChatId}&part=id%2Csnippet%2CauthorDetails{(_nextPageToken!=""? $"&pageToken={_nextPageToken}" : "")}"); web.Method = "GET"; web.Headers.Add("Authorization", $"{YouTubeOAuthToken.tokenType} {YouTubeOAuthToken.accessToken}"); web.Accept = "application/json"; using (HttpWebResponse resp = (HttpWebResponse)web.GetResponse()) { if (resp.StatusCode == HttpStatusCode.OK) { using (Stream dataStream = resp.GetResponseStream()) { using (StreamReader reader = new StreamReader(dataStream)) { string ret = reader.ReadToEnd(); Process(ret); //Plugin.Log($"Chat: {ret}"); } } } else { Plugin.Log($"Error: {resp.StatusCode.ToString()}"); } } } catch (WebException ex) { // Read the response and log it using (Stream dataStream = ex.Response.GetResponseStream()) { using (StreamReader reader = new StreamReader(dataStream)) { var response = reader.ReadToEnd(); Plugin.Log($"Status: {ex.Status}, Response: {response}"); switch (((HttpWebResponse)ex.Response).StatusCode) { // If we hit an unauthorized exception, the users auth token has expired case HttpStatusCode.Unauthorized: // Try to refresh the users auth token, forcing it through even if our local timestamp says it's not expired if (!YouTubeOAuthToken.Refresh(true)) { YouTubeOAuthToken.Invalidate(); YouTubeOAuthToken.Generate(); } break; case HttpStatusCode.Forbidden: var json = JSON.Parse(response); List <string> errorList = new List <string>(); foreach (var error in json["error"]["errors"].AsArray) { errorList.Add(error.Value["reason"].Value); } YouTubeConnection.lastError = "Unable to retrieve YouTube live chat messages. Have you reached your quota? Error(s): " + string.Join(", ", errorList); YouTubeMessageHandlers.InvokeHandler(new YouTubeMessage("youtube#onError"), ""); YouTubeLiveBroadcast.currentBroadcast = null; YouTubeConnection.Stop(); break; } } } _pollingIntervalMillis = 3000; } catch (Exception ex) { Plugin.Log(ex.ToString()); _pollingIntervalMillis = 3000; } Thread.Sleep(_pollingIntervalMillis); }
internal static void Refresh() { try { Plugin.Log($"Requesting live broadcast info..."); HttpWebRequest web = (HttpWebRequest)WebRequest.Create("https://www.googleapis.com/youtube/v3/liveBroadcasts?part=id%2Csnippet%2Cstatus&broadcastStatus=all&broadcastType=all&maxResults=50"); web.Method = "GET"; web.Headers.Add("Authorization", $"{YouTubeOAuthToken.tokenType} {YouTubeOAuthToken.accessToken}"); web.Accept = "application/json"; web.UserAgent = "StreamCoreClient"; using (HttpWebResponse resp = (HttpWebResponse)web.GetResponse()) { if (resp.StatusCode == HttpStatusCode.OK) { using (Stream dataStream = resp.GetResponseStream()) { using (StreamReader reader = new StreamReader(dataStream)) { string ret = reader.ReadToEnd(); if (Update(ret)) { channelName = GetChannelName(currentBroadcast.snippet.channelId); Plugin.Log("YouTube channel name: " + channelName); YouTubeMessageHandlers.InvokeHandler(new YouTubeMessage("youtube#onConnectedToLiveChat"), ""); TaskHelper.CancelTask("YouTubeChannelRefresh"); Plugin.Log($"There are currently {broadcasts.Count} broadcasts being tracked.");// Ret: {ret}"); } //Plugin.Log($"Broadcast \"{broadcast.Value.snippet.title}\" (ID: {broadcast.Value.id}, ChannelID: {broadcast.Value.snippet.channelId}) with description \"{broadcast.Value.snippet.description}\" status is \"{broadcast.Value.status.recordingStatus}\""); } } } else { Plugin.Log($"Error: {resp.StatusCode}"); } } } catch (WebException ex) { // Read the response and log it using (Stream dataStream = ex.Response.GetResponseStream()) { using (StreamReader reader = new StreamReader(dataStream)) { var response = reader.ReadToEnd(); Plugin.Log($"Status: {ex.Status}, Response: {response}"); switch (((HttpWebResponse)ex.Response).StatusCode) { // If we hit an unauthorized exception, the users auth token has expired case HttpStatusCode.Unauthorized: // Try to refresh the users auth token, forcing it through even if our local timestamp says it's not expired if (!YouTubeOAuthToken.Refresh(true)) { YouTubeOAuthToken.Invalidate(); YouTubeOAuthToken.Generate(); } break; case HttpStatusCode.Forbidden: var json = JSON.Parse(response); List <string> errorList = new List <string>(); foreach (var error in json["error"]["errors"].AsArray) { errorList.Add(error.Value["reason"].Value); } YouTubeConnection.lastError = "Unable to retrieve live broadcast data! Ensure you have configured the YouTube data API correctly, then try again. Error(s): " + string.Join(", ", errorList); YouTubeMessageHandlers.InvokeHandler(new YouTubeMessage("youtube#onError"), ""); currentBroadcast = null; YouTubeConnection.Stop(); break; } } } } catch (Exception ex) { Plugin.Log(ex.ToString()); } }