Davincible / goinsta

Unofficial Instagram API written in Golang (v2022)
MIT License
182 stars 54 forks source link

goinsta new #12

Closed Oyetomi closed 2 years ago

Oyetomi commented 2 years ago

hello thanks for maintaining this repo, im wrting some functions and i always have to do a goinsta.new to login to perform an action and how can i get around this and logging in with cookies dont work also. thanks

Davincible commented 2 years ago

Hi there, thanks for using it! Could you be a bit more specific? Per run time you need to create one goinsta.Instagram instance per user. If there are no existing cookies you do this by calling goinsta.New + goinsta.Login, these should only be called once per user or if your cookies completely expired (rarely happens). If there are cookies you create the instance by calling only goinsta.Import, you also only need to this once per user per runtime however. You can then continue to use the insta instance throughout your program

Oyetomi commented 2 years ago

Hi there, thanks for using it! Could you be a bit more specific? Per run time you need to create one goinsta.Instagram instance per user. If there are no existing cookies you do this by calling goinsta.New + goinsta.Login, these should only be called once per user or if your cookies completely expired (rarely happens). If there are cookies you create the instance by calling only goinsta.Import, you also only need to this once per user per runtime however. You can then continue to use the insta instance throughout your program

thanks for your response , cookies always fail to login for me

Oyetomi commented 2 years ago

sorry for the bad code . this is better , i can login with goinsta.new , but cant login with insta.import , sessions isn't working . the session is created successfully by the initial login but i can login with the cookies itself to perform actions, i hope you understand

func Login(username,password string) {
        _, err := os.Stat("./internal/session")
        if !os.IsNotExist(err) {
            insta, err := goinsta.Import("./internal/session")
            if err != nil {
                util.CheckError(errors.ErrCouldNotImportSession)
            }
            err = insta.Login()
            if err != nil {
            util.CheckError(errors.ErrLoginFailed)
            } else {
            fmt.Println("Successfully Logged In Via Session...")
            }
        } else {
            insta := goinsta.New(username,password)
            err := insta.Login()
            if err != nil {
                util.CheckError(errors.ErrSessionNotFound)
            } else {
                fmt.Println("Successfully logged in...")
                fmt.Println("Creating Session...")
                err = insta.Export("./internal/session")
                if err != nil {
                    util.CheckError(errors.ErrCouldNotCreateSession)
                } else {
                    fmt.Println("Session created successfully..")
                }
            }
        }
}
Davincible commented 2 years ago

Yes you are calling insta.Login after loading the session, that is not necessary. You only need to call login the first time, not after loading a session. Perhaps that could be explained a bit more clearly in the wiki, I will amend that.

Also, you don't need to manually check for the file existence, just call import, and if it returns an error the file doesn't exist.

Oyetomi commented 2 years ago

Yes you are calling insta.Login after loading the session, that is not necessary. You only need to call login the first time, not after loading a session. Perhaps that could be explained a bit more clearly in the wiki, I will amend that.

Also, you don't need to manually check for the file existence, just call import, and if it returns an error the file doesn't exist.

thank you for your explanation