ramtinak / InstagramApiSharp

A complete Private Instagram API for .NET (C#, VB.NET).
MIT License
789 stars 239 forks source link

NullReferenceException when trying to login on macOS and iOS (MAUI) #586

Closed IcySnex closed 3 months ago

IcySnex commented 1 year ago

I've:

Issue category

Language

Usage

Operating System

Debug logs

macOS and iOS: 2023-04-01 14:10:27.507 MyApp[1867:31285] Exception: System.NullReferenceException: Object reference not set to an instance of an object. at InstagramApiSharp.API.InstaApi.LoginAsync(Boolean isNewLogin) 2023-04-01 14:10:27.509 MyApp[1867:31285] Stacktrace: at InstagramApiSharp.API.InstaApi.LoginAsync(Boolean isNewLogin)

Describe your issue

This API wrapper is not correctly working on macOS or iOS while using MAUI. Whenever trying to login with an Instagram account the API throws an exception: "Object reference not set to an instance of an object". Tested the same application on Xamarin and it works. This is the code I am currently using:

EntryInput.Text += "Starting...\n";
IInstaApi Api = InstaApiBuilder.CreateBuilder()
    .SetUser(UserSessionData
        .ForUsername("USER_NAME")
        .WithPassword("USER_PASSWORD"))
    .UseLogger(new DebugLogger(LogLevel.Exceptions))
    .Build();
EntryInput.Text += "Created API instance\n";

EntryInput.Text += "Loging in...\n";
var logInResult = await Api.LoginAsync();
EntryInput.Text += $"Fínished loging in: {logInResult.Info.Message}\n";

if (!logInResult.Succeeded)
{
    EntryInput.Text += $"Failed loging in: {logInResult.Info.Exception.Message}\n";
    return;
}

EntryInput.Text += $"Getting current user...\n";
var user = await Api.GetCurrentUserAsync();
EntryInput.Text += $"Got current user: {user.Info.Message}\n";

if (!user.Succeeded)
{
    EntryInput.Text += $"Failed getting user: {user.Info.Exception.Message}\n";
    return;
}

EntryInput.Text += $"User: {user.Value.UserName}\n";

As a quick note, this code on MAUI is fully working on Windows and Android. I dont know if this is a problem with my project setup but I am just using an empty template.

ramtinak commented 1 year ago

Hey, I think you have to update the csproj to net6.0 or 7.0, it might help you, because for now it's just netstandard2.0.

Also check for Bouncy Castle library or other libraries as well.

I don't have macos nor ios, so can't help you.

P.s: i accidently closed this, sorry.

IcySnex commented 1 year ago

Normally netstandard2.0 packages support all net versions and frameworks including net6.0 and net7.0 and I mean it works on Windows and Android with MAUI. I think MAUI on iOS and macOS currently doesn't support on of the used dependencies by the wrapper. I can't really debug the wrapper to find the exact issue because I am using macOS in a virtual machine and the performance is AWFUL. Im just using it to build .ipa files for iOS.

I really wanted to try this new MAUI thing everyone is hyped about for the app I'm currently developing but I have already switched to Xamarin because of this issue and Xamarin is fine.

Also Two-Factor Authorization Login is not correctly working. It keeps saying "Check security code" even tho it is correct. Tested with 2FA-App and 2FA-SmS options enabled on the Instagram Account. I used the sample project on Windows.

MCvel commented 1 year ago

I have the same issue with Macos and iOS, does anybody found any workaround?

IcySnex commented 1 year ago

I did not find any workaround. Only thing you can do is switch API wrappers (which is very hard since there arent really any updated usable wrappers) or use Xamarin instead of MAUI.