Failing to connect to steam with SteamKit2

3.2k views Asked by At

I am making a trade offer bot in C# using SteamKit2, and most of the time it is successfully connecting to steam. But some of the time it just freezes when I have it output "Connecting to Steam..." right before client.connect(); is called. It happens often enough that it needs to be fixed, but I don't know what the problem is. Here is my code (a lot was taken from a SteamKit2 tutorial):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SteamKit2;

namespace ATO
{
    class OfferSender
    {
        string username;
        string password;

        SteamClient client;
        CallbackManager manager;
        SteamUser user;

        bool isRunning = false;

        public OfferSender()
        {

        }

        public void login()
        {
            Console.Write("Please enter your username: ");
            username = Console.ReadLine();

            Console.Write("Please enter your password: ");
            password = Console.ReadLine();


            client = new SteamClient();

            manager = new CallbackManager(client);

            user = client.GetHandler<SteamUser>();

            new Callback<SteamClient.ConnectedCallback>(OnConnected, manager);

            new Callback<SteamUser.LoggedOnCallback>(OnLoggedOn, manager);

            isRunning = true;

            Console.WriteLine("\nConnecting to Steam...\n");

            client.Connect();



            while(isRunning)
            {
                manager.RunWaitCallbacks(TimeSpan.FromSeconds(1));
            }
            Console.ReadKey();
        }

        public void OnConnected(SteamClient.ConnectedCallback callback)
        {
            if (callback.Result != EResult.OK)
            {
                Console.WriteLine("Error connecting to Steam: {0}", callback.Result);
                isRunning = false;
                return;
            }

            Console.WriteLine("Connected to steam.\nLogging in {0}...\n", username);

            user.LogOn(new SteamUser.LogOnDetails {
                Username = username,
                Password = password
            });
        }

        public void OnLoggedOn(SteamUser.LoggedOnCallback callback)
        {
            if (callback.Result == EResult.AccountLogonDenied)
            {
                Console.WriteLine("This account is SteamGuard protected.");
                return;
            }
            if(callback.Result != EResult.OK)
            {
                Console.WriteLine("Unable to log in to steam {0}\n", callback.Result);
                isRunning = false;
                return;
            }
            Console.WriteLine("successfully logged in!");
            Environment.Exit(0);
        }

    }
}

How do I fix this?

3

There are 3 answers

0
Michal Varyš On

You need to handle some more callbacks.

Here is how do I log into steam with my bot.

SteamBot.cs

using System;
using SteamKit2;
using System.Collections.Generic;

namespace StackOverflow
{
    class SteamBot
    {
        CallbackManager             m_CallbackManager;
        SteamUser                   m_SteamUser;
        SteamClient                 m_SteamClient;
        SteamFriends                m_SteamFriends;
        SteamID                     m_SteamID;
        Dictionary<string, string>  m_Config;

        public bool isLoggedOn { get; private set; }
        public bool isReady { get; private set; }

        public SteamBot(string pUsername, string pPassword)
        {
            isLoggedOn = false;
            isReady = false;
            m_Config = new Dictionary<string, string>();

            m_Config.Add("username", pUsername);
            m_Config.Add("password", pPassword);

            Init();
            RegisterCallbacks();
            Connect();            
        }

        private void Init()
        {
            m_SteamClient = new SteamClient();
            m_CallbackManager = new CallbackManager(m_SteamClient);

            m_SteamFriends = m_SteamClient.GetHandler<SteamFriends>();
            m_SteamUser = m_SteamClient.GetHandler<SteamUser>();
        }

        private void RegisterCallbacks()
        {
            m_CallbackManager.Subscribe<SteamClient.ConnectedCallback>(OnConnected);
            m_CallbackManager.Subscribe<SteamClient.DisconnectedCallback>(OnDisconnected);

            m_CallbackManager.Subscribe<SteamUser.LoggedOnCallback>(OnLoggedOn);
            m_CallbackManager.Subscribe<SteamUser.LoggedOffCallback>(OnLoggedOff);

            m_CallbackManager.Subscribe<SteamUser.AccountInfoCallback>(OnAccountInfo);
        }

        public void WaitForCallbacks()
        {
            m_CallbackManager.RunWaitCallbacks(TimeSpan.FromSeconds(1));
        }

        public string GetConfigValue(string pKey)
        {
            return m_Config[pKey];
        }

        public void Connect()
        {
            m_SteamClient.Connect();
            isReady = true;
        }

        void OnConnected(SteamClient.ConnectedCallback pData)
        {
            Console.WriteLine("Connected to Steam! Logging in '{0}'...", GetConfigValue("username"));

            SteamUser.LogOnDetails details = new SteamUser.LogOnDetails
            {
                Username = GetConfigValue("username"),
                Password = GetConfigValue("password"),
            };

            m_SteamUser.LogOn(details);
            m_SteamID = m_SteamClient.SteamID;
        }

        void OnDisconnected(SteamClient.DisconnectedCallback pData)
        {
            m_SteamClient.Disconnect();
        }

        void OnLoggedOff(SteamUser.LoggedOffCallback pData)
        {
            isLoggedOn = false;
        }

        void OnLoggedOn(SteamUser.LoggedOnCallback pData)
        {
            if (pData.Result != EResult.OK)
            {
                Console.WriteLine("Unable to login to Steam: {0} / {1}", pData.Result, pData.ExtendedResult);
                return;
            }

            Console.WriteLine("Successfully logged on!");
            isLoggedOn = true;
        }
        void OnAccountInfo(SteamUser.AccountInfoCallback pData)
        {
            //Announce to all friends that we are online
            m_SteamFriends.SetPersonaState(EPersonaState.Online);
        }
    }
}

Program.cs

namespace StackOverflow
{
    class Program
    {
        static void Main(string[] args)
        {
            SteamBot bot = new SteamBot("botname", "password");

            while(true)
            {
                if(bot.isReady)
                {
                    bot.WaitForCallbacks();
                }
            }
        }
    }
}
0
TEBco01 On

You need to add Steam Directory.Initialize().Wait(); before you attempt to connect so SteamKit can update its internal list of servers, as explained here.

1
Luiz Vaz On

Well, after banging my head against the keyboard I finaly spoted that Steam would be requesting an AuthCode like any other new computer or browser login.

And that's it...

The code below handles either AuthCode and TwoFactor authentications: https://github.com/SteamRE/SteamKit/tree/master/Samples/5.SteamGuard