Need help on loading data

SaeedP

Well-known member
Joined
Oct 21, 2020
Messages
97
Programming Experience
3-5
Hi,

I'm doing a web app in blazor for Instagram. I use instaApiSharp:

Should I use load state from stream or loading state from String??

When I use " loadStateFromStream" ,
When I run nothing happens and just I see:
this message: Message: Loading state from file
and there is no exception.

C#:
if (File.Exists(stateFile))
    
             {
                    
                 message = "Loading state from file";
    
                 using (var fs = File.OpenRead(stateFile))
    
                 {
                        
                     _instaApi.LoadStateDataFromStream(fs);
    
                 }
             }
         }

and when using"loadStateFromString":

C#:
if (File.Exists(stateFile))
             {
                    
                 message = "Loading state from file";
    
                 using (var fs = File.OpenRead(stateFile))
                 {
                    _instaApi.LoadStateDataFromString(new StreamReader(fs).ReadToEnd());
                        
                 }

I encounter this exception:

ExceptionMessage: error: Newtonsoft.Json.JsonReaderException: Unexpected character encountered while parsing value: . Path '', line 1, position 1. at Newtonsoft.Json.JsonTextReader.ParseValue() at Newtonsoft.Json.JsonReader.ReadAndMoveToContent() at Newtonsoft.Json.JsonReader.ReadForType(JsonContract contract, Boolean hasConverter) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent) at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType) at Newtonsoft.Json.JsonSerializer.Deserialize(JsonReader reader, Type objectType) at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings) at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonSerializerSettings settings) at InstagramApiSharp.API.InstaApi.LoadStateDataFromString(String json) at InstaSite1.Pages.Enterance.StartInstagram() in

I'm doing my project in blazor and the API recommends to
"loadStateFromString"


You can see the whole code here:

C#:
@code{

    public static IInstaApi _instaApi;

    public static string username { get; set; }
    public static string password { get; set; }

    public static string message = string.Empty;

    public static string ExceptionMessage = string.Empty;



    private static async Task StartInstagram()
    {
        var userSession = new UserSessionData
        {
            UserName = Enterance.username,
            Password = Enterance.password
        };

        var _instaApi = InstaApiBuilder.CreateBuilder()
    .SetUser(userSession)
    .UseLogger(new DebugLogger(LogLevel.Exceptions))
    .Build();


        const string stateFile = "state.bin";
        try
        {
            // load session file if exists
            if (File.Exists(stateFile))
            {
                //Console.WriteLine("message = "";");
                message = "Loading state from file";

                using (var fs = File.OpenRead(stateFile))
                {
                    //_instaApi.LoadStateDataFromStream(fs);
                    //_instaApi.LoadStateDataFromString(new StreamReader(fs).ReadToEnd());
                    ////////////////////////////////////////////////////////////////////

                    //_instaApi.LoadStateDataFromString(new StreamReader(fs).ReadToEnd());

                    _instaApi.LoadStateDataFromStream(fs);

                }
            }
        }

        catch (Exception e)
        {
            ExceptionMessage = $" error: {e}";
        }
        if (!_instaApi.IsUserAuthenticated)
        {
            // login


            message = $"Logging in as {userSession.UserName}";
            var logInResult = await _instaApi.LoginAsync();
            if (!logInResult.Succeeded)
            {
                //Console.WriteLine($"Unable to login: {logInResult.Info.Message}");
                message = $"Unable to login: {logInResult.Info.Message}";
                return;
            }
        }

        // save session in file
        var state = _instaApi.GetStateDataAsStream();

        using (var fileStream = File.Create(stateFile))
        {
            state.Seek(0, SeekOrigin.Begin);
            state.CopyTo(fileStream);
        }

    }



}


What is your idea? Please inform me.

Regards,

Saeed


What is your idea?

Saeed
 
I have made this app with another library: instasharper

On that app, when I run it and when I want to connect to Instagram, there is no exception.

But I get this message: Challenge is required.

But is your idea about this one?
 
Step through the code of the library to find out.
 
Back
Top Bottom