0

So I am using InstaSharper, and I want to make the user to type in their name of target user, however, Instead of processing the data, it just does nothing at all. Here's my code:

    class Program
{

    //username and pass
    #region Hidden
    #endregion
    private static UserSessionData user;
    private static IInstaApi api;
    static void Main(string[] args)
    {
        string inputUser;
        string inputPass;

        Console.WriteLine("Enter the username");
        inputUser = Console.ReadLine();
        Console.WriteLine("Enter your password:");
        inputPass = Console.ReadLine();

        user = new UserSessionData();
        user.UserName = inputUser;
        user.Password = inputPass;

        Login();
        Console.Read();
    }

    public static async void Login()
    {
        //login attempt

        api = InstaApiBuilder.CreateBuilder()
            .SetUser(user)
            .UseLogger(new DebugLogger(LogLevel.Exceptions))
            .Build();

        var loginRequest = await api.LoginAsync();
        if (loginRequest.Succeeded)
        {
            string targetUser;
            Console.WriteLine("Logged in!\n\t");
            Console.WriteLine("Now enter the target user");
            targetUser = Console.ReadLine();
            PullUserPosts(targetUser);
        }
        else
        {
            Console.WriteLine("Error logging in, are you sure you have entered the right credentials?");
        }

    }

    public static async void PullUserPosts(string userToScrape)
    {
        //instagram show followers demo
        var userinfo = await api.GetUserInfoByIdAsync(123456789);
        Console.WriteLine($"USER: {userinfo.Value.FullName}\n\tFollowers: {userinfo.Value.FollowerCount} \n \t Is it verified?\n\t {userinfo.Value.IsVerified}\n\t\n\t");

        Console.WriteLine("Loading all posts and captions by target user.. \n\t");

        IResult<InstaMediaList> media = await api.GetUserMediaAsync(userToScrape, PaginationParameters.MaxPagesToLoad(5));
        List<InstaMedia> mediaList = media.Value.ToList();

        for (int i = 0; i < mediaList.Count; i++)
        {
            InstaMedia m = mediaList[i];
            if (m != null && m.Caption != null)
            {
                string captionText = m.Caption.Text;
                if (captionText != null)
                {
                    if (m.MediaType == InstaMediaType.Image)
                    {
                        for (int x = 0; x < m.Images.Count; x++)
                        {
                            if (m.Images[x] != null && m.Images[x].URI != null)
                            {
                                Console.WriteLine($"\n\t{captionText}\n\t");
                                string uri = m.Images[x].URI;

                                Console.Write($"{uri}\n\t");
                            }
                        }
                    }
                }
            }
        }

it manages to log in, but when I tell user to "Now enter the target user", nothing happened. Does anyone know how to fix it?

  • I see couple of problems if your code. First, never do async `void`, always return async `Task`. Second, is you do not `await` your Login method in Main. Change Main return from `void` to `Task` and await `Login` – OlegI Nov 30 '19 at 08:45
  • @Olegl thanks for the advice, though still no luck in fixing the problem (doesn't do anything on user input, as I said earlier, it just closes automatically after compiling and doesn't do anything when set it to debugging mode). By the way can't remove await from login, an error will pop up "CS1061 'Task>' does not contain a definition for 'Succeeded' and no accessible extension method 'Succeeded' accepting a first argument of type 'Task>' could be found (are you missing a using directive or an assembly reference?)" – invalid232 Nov 30 '19 at 08:58

0 Answers0