public async Task <ActionResult> OAuthAuthorize() { var reqCode = HttpContext.Request.Query["code"]; var reqState = HttpContext.Request.Query["state"]; var requestUrl = "https://www.ravenfall.stream/login"; try { var sessionInfo = await TwitchAuthenticateAsync(reqCode); if (sessionInfo != null) { requestUrl += "?token=" + sessionInfo.access_token + "&state=" + reqState; var req = new TwitchRequests(sessionInfo.access_token, settings.TwitchClientId, settings.TwitchClientSecret); var info = await req.ValidateOAuthTokenAsync(); if (info != null) { requestUrl += "&id=" + info.ClientID + "&user=" + info.Login; } } } catch { // ignored } return(Redirect(requestUrl)); }
public async Task <ActionResult> GetChannelPictureAsync(string userId) { try { if (memoryCache != null && memoryCache.TryGetValue("logo_" + userId, out var logoData) && logoData is byte[] data) { return(File(data, "image/png")); } var twitch = new TwitchRequests(clientId: settings.TwitchClientId, clientSecret: settings.TwitchClientSecret); var profile = await twitch.GetUserAsync(userId); if (profile != null) { using (var wc = new WebClient()) { var binaryData = await wc.DownloadDataTaskAsync(new Uri(profile.logo)); return(File(memoryCache.Set("logo_" + userId, binaryData), "image/png")); } } return(NotFound()); } catch { return(NotFound()); } }
public async Task <TwitchRequests.TwitchUser> GetTwitchUserAsync(string sessionId, string token = null) { var str = GetString(sessionId, TwitchUser); if (string.IsNullOrEmpty(str) && !string.IsNullOrEmpty(token)) { var twitch = new TwitchRequests(token, settings.TwitchClientId, settings.TwitchClientSecret); var user = await twitch.GetUserAsync(); if (user != null) { await SetTwitchUserAsync(sessionId, user); } str = user; } if (string.IsNullOrEmpty(str)) { return(null); } try { return(JSON.Parse <TwitchRequests.TwitchUserData>(str).Data?.FirstOrDefault()); } catch (Exception exc) { logger.LogError("GET TWITCH USER (" + sessionId + "): " + str + " --- PARSE ERROR (EXCEPTION): " + exc); return(JSON.Parse <TwitchRequests.TwitchUser>(str)); } }
public async Task <byte[]> GetChannelPictureAsync(string userId) { try { var forceRefreshLogo = userId.Contains("_"); if (forceRefreshLogo) { userId = userId.Split('_')[1]; } else { if (memoryCache != null && memoryCache.TryGetValue("logo_" + userId, out var logoData) && logoData is byte[] data) { return(data); } } var twitch = new TwitchRequests(clientId: settings.TwitchClientId, clientSecret: settings.TwitchClientSecret); var profile = await twitch.Kraken_GetUserAsync(userId); if (profile != null) { using (var wc = new WebClient()) { var binaryData = await wc.DownloadDataTaskAsync(new Uri(profile.logo)); return(memoryCache.Set("logo_" + userId, binaryData)); } } } catch { } return(null); }
public async Task <byte[]> GetClanLogoAsync(string userId) { try { string logoUrl = null; var forceRefreshLogo = userId.Contains("_"); if (forceRefreshLogo) { userId = userId.Split('_')[1]; var user = gameData.GetUserByTwitchId(userId); if (user != null) { var clan = gameData.GetClanByUser(user.Id); if (clan != null && clan.Logo != null && clan.Logo.Contains("/api/twitch/logo/")) { clan.Logo = null; } logoUrl = clan?.Logo; } } else { if (memoryCache != null && memoryCache.TryGetValue("clan_logo_" + userId, out var logoData) && logoData is byte[] data) { return(data); } } if (string.IsNullOrEmpty(logoUrl)) { var twitch = new TwitchRequests(clientId: settings.TwitchClientId, clientSecret: settings.TwitchClientSecret); var profile = await twitch.Kraken_GetUserAsync(userId); if (profile != null) { logoUrl = profile.logo; } } if (!string.IsNullOrEmpty(logoUrl)) { using (var wc = new WebClient()) { var binaryData = await wc.DownloadDataTaskAsync(new Uri(logoUrl)); return(memoryCache.Set("clan_logo_" + userId, binaryData)); } } } catch { } return(null); }
public async Task <string> GetTwitchUser() { if (!this.sessionInfoProvider.TryGetTwitchToken(HttpContext.Session, out var key)) { return("nope"); } var twitch = new TwitchRequests(key); var twitchUser = await twitch.GetUsersAsync(); await this.sessionInfoProvider.SetTwitchUserAsync(HttpContext.Session, twitchUser); return(twitchUser); }
public async Task <string> GetTwitchUser() { if (sessionInfoProvider.TryGet(HttpContext.Session, out var session)) { return($"{{ \"login\": \"{session.UserName}\", \"id\": \"{session.UserId}\"}}"); } if (!this.sessionInfoProvider.TryGetTwitchToken(HttpContext.Session, out var key)) { return("nope"); } var twitch = new TwitchRequests(key, settings.TwitchClientId, settings.TwitchClientSecret); var twitchUser = await twitch.GetUserAsync(); await this.sessionInfoProvider.SetTwitchUserAsync(HttpContext.Session, twitchUser); return(twitchUser); }