public static async Task APIRequest() { var apiKey = "API KEY"; var json = "SESSION JSON CONTENT"; var webSession = JsonConvert.DeserializeObject <WebSession>(json); if (webSession?.HasEnoughInfo() == true) { var webAccess = new SteamWebAccess(webSession); var webAPI = new SteamWebAPI(apiKey, webAccess); var tf2News = await webAPI.RequestDynamic( "ISteamNews", SteamWebAccessRequestMethod.Get, "GetNewsForApp", "v2", new { appid = 440 } ); // Show tf2News } }
public static async Task GetAPIKey() { var json = "SESSION JSON CONTENT"; var webSession = JsonConvert.DeserializeObject <WebSession>(json); if (webSession?.HasEnoughInfo() == true) { var webAccess = new SteamWebAccess(webSession); var apiKey = await SteamWebAPI.GetApiKey(webAccess); if (string.IsNullOrWhiteSpace(apiKey)) { if (await SteamWebAPI.RegisterApiKey(webAccess, "www.example.com")) { apiKey = await SteamWebAPI.GetApiKey(webAccess); } else { // Failed to register a new API key for this account } } if (!string.IsNullOrWhiteSpace(apiKey)) { // Retrieved a valid API key associated with the account represented by the passed WebSession // Use this API key to create an instance of SteamWebAPI or just save it somewhere for later use var webAPI = new SteamWebAPI(apiKey, webAccess); } } }
protected virtual async Task <bool> GetGuestSession() { // Get a new SessionId SteamWebAccess = SteamWebAccess.GetGuest(); (await OperationRetryHelper.Default .RetryOperationAsync(() => SteamWebAccess.FetchBinary(new SteamWebAccessRequest(LoginInitializeUrl))) .ConfigureAwait(false)).Dispose(); return(!string.IsNullOrWhiteSpace(SteamWebAccess?.Session?.SessionId)); }
protected virtual void ResetStates() { // Reset variables SteamWebAccess = null; RequiresCaptchaCode = false; RequiresTwoFactorAuthenticationCode = false; RequiresEmailVerification = false; CaptchaGID = null; CachedCaptchaImage = null; SteamId = null; }
public static async Task VerifySession() { var json = "SESSION JSON CONTENT"; var webSession = JsonConvert.DeserializeObject <WebSession>(json); if (webSession?.HasEnoughInfo() == true) { var webAccess = new SteamWebAccess(webSession); if (await webAccess.VerifySession()) { // Session is valid } else { // Session expired, please login again } } }
public SteamWebAPI(SteamWebAccess steamWebAccess) : this(null, steamWebAccess) { }
public SteamWebAPI(string apiKey, SteamWebAccess steamWebAccess) { _apiKey = apiKey; SteamWebAccess = steamWebAccess ?? SteamWebAccess.GetGuest(); }
protected virtual Task <bool> ProcessLoginResponse(string response) { var loginResponse = JsonConvert.DeserializeObject <LoginResponse>(response); if (loginResponse == null) { throw new UserLoginException(UserLoginErrorCode.GeneralFailure, this); } if (loginResponse.Message?.ToLower().Contains("incorrect login") == true || loginResponse.Message?.ToLower().Contains("password") == true && loginResponse.Message?.ToLower().Contains("incorrect") == true) { throw new UserLoginException(UserLoginErrorCode.BadCredentials, this); } if (loginResponse.CaptchaNeeded) { RequiresCaptchaCode = true; CaptchaGID = loginResponse.CaptchaGID; CachedCaptchaImage = null; throw new UserLoginException(UserLoginErrorCode.NeedsCaptchaCode, this); } if (!string.IsNullOrWhiteSpace(loginResponse.CaptchaGID) && loginResponse.CaptchaGID != "-1" && CaptchaGID != loginResponse.CaptchaGID) { CaptchaGID = loginResponse.CaptchaGID; CachedCaptchaImage = null; } if (loginResponse.EmailAuthNeeded) { RequiresEmailVerification = true; SteamId = loginResponse.EmailSteamId > 0 ? loginResponse.EmailSteamId : SteamId; EmailDomain = loginResponse.EmailDomain; throw new UserLoginException(UserLoginErrorCode.NeedsEmailVerificationCode, this); } if (loginResponse.TwoFactorNeeded && !loginResponse.Success) { RequiresTwoFactorAuthenticationCode = true; SteamId = loginResponse.EmailSteamId > 0 ? loginResponse.EmailSteamId : SteamId; throw new UserLoginException(UserLoginErrorCode.NeedsTwoFactorAuthenticationCode, this); } if (loginResponse.EmailSteamId > 0 && SteamId != loginResponse.EmailSteamId) { SteamId = loginResponse.EmailSteamId; } if (loginResponse.Message?.Contains("too many login failures") == true) { throw new UserLoginException(UserLoginErrorCode.TooManyFailedLoginAttempts, this); } if (!loginResponse.LoginComplete) { return(Task.FromResult(false)); } if (loginResponse.TransferParameters != null && !string.IsNullOrWhiteSpace(SteamWebAccess?.Session?.SessionId)) { var newSession = new WebSession(loginResponse.TransferParameters, SteamWebAccess.Session.SessionId); if (newSession.HasEnoughInfo()) { SteamWebAccess = new SteamWebAccess(newSession); } } return(Task.FromResult(true)); }