TheM4hd1 / SwiftyInsta

Instagram Private API Swift
MIT License
225 stars 51 forks source link

Ask about credentials.code #115

Closed rmelnik7777 closed 4 years ago

rmelnik7777 commented 5 years ago

Hello, Please, tell me. I have a problem.

With request in func login, I get an SMS with the code, that I registered in credentials.code = "121332". I can’t send this code back. How to do it? What am I doing wrong?

This is my code

class ViewController: UIViewController {

let handler = APIHandler()    
override func viewDidLoad() {
    super.viewDidLoad()
    login()
}

func login() {
    var credentials = Credentials(username: "username", password: "password", verifyBy: .text)
    credentials.code = "121332"

    handler.authenticate(with: .user(credentials)) { [weak self] in
        switch $0 {
        case .success(let response, _):
            print("Login successful.")
            guard let key = response.persist() else { return print("`Authentication.Response` could not be persisted.") }
            UserDefaults.standard.set(key, forKey: "current.account")
            UserDefaults.standard.synchronize()

            self?.getCurrentUser()
        case .failure(let error):
            if error.requiresInstagramCode {
            } else {
            }
        }
    }
}

func getCurrentUser() {
    self.handler.users.user(.me) { ( result: Result<User?, Error>) in
        switch result {
        case .success(let user):
            let username = user?.name ?? "John Doe"
            print("success, username: \(username)")
        case .failure(let error):
            print("failure, error: \(error.localizedDescription)")
        }
    }
}

}

sbertix commented 5 years ago

You need to move credentials.code = "121332" after the authentication handler returns .failure(let error). Doing it async inside the if error.requiresInstagramCode { } might be a good option.

rmelnik7777 commented 5 years ago

Problem still exists https://prnt.sc/pgyf15 apparently does not send this code

sbertix commented 5 years ago

Try without the breakpoint. Cause this way you're actually stopping the code as it reaches that row, so it can't propagate any request as credentials.code changes. :blush: If it still doesn't change anything, try retaining credentials too (although it shouldn't be an issue from what I can see from your snippet, tbh).

rmelnik7777 commented 5 years ago

https://prnt.sc/pgynah Problem still exists. I would like to draw your attention, I pass the credentials to .user, because if there is .credentials (as in the example), then an error occurs

sbertix commented 5 years ago

.user is the correct way. .credentials was used in development and I forgot to change it. Thanks for pointing out. I'll change it right now. I still don't get the error though cause I'm testing it my way and everything works as intended 🤔

sbertix commented 5 years ago

Since I can't see it in your last screenshot, does it really not work with persisting the credentials that way, and then updating code after it returns the Error requiring it? Cause that appears to be the only thing you're doing different, tbh 😞

rmelnik7777 commented 5 years ago

If I us .credentials, i have this error https://prnt.sc/pgyvlx I posted all my code in the main message. Am I doing something wrong?

rmelnik7777 commented 5 years ago

how do you test sending a message back?

sbertix commented 5 years ago

.user is the right one, .credentials was kept in the documentation by mistake. Move your declaration of credentials below the handler one. As a "retained" property. It should work then.

let handler = APIHandler()
let credentials = Credentials( … )

func login() {
  …
}
rmelnik7777 commented 5 years ago

its all code, there is an error anyway (

import UIKit import SwiftyInsta

class ViewController: UIViewController { let handler = APIHandler() var credentials = Credentials(username: "username", password: "password", verifyBy: .text)

override func viewDidLoad() {
    super.viewDidLoad()
    login()
}

func login() {

    handler.authenticate(with: .user(credentials)) { [weak self] in
        switch $0 {
        case .success(let response, _):
            print("Login successful.")
            guard let key = response.persist() else { return print("`Authentication.Response` could not be persisted.") }
            UserDefaults.standard.set(key, forKey: "current.account")
            UserDefaults.standard.synchronize()
            self?.getCurrentUser()
        case .failure(let error):
            if error.requiresInstagramCode {
                self!.credentials.code = "178063"
            } else {
            }
        }
    }
}

func getCurrentUser() {
    self.handler.users.user(.me) { ( result: Result<User?, Error>) in
        switch result {
        case .success(let user):
            let username = user?.name ?? "John Doe"
            print("success, username: \(username)")
        case .failure(let error):
            print("failure, error: \(error.localizedDescription)")
        }
    }
}

}

sbertix commented 5 years ago

And what is the error this time? @rmelnik7777

rmelnik7777 commented 5 years ago

2019-10-09 17:32:25.333892+0300 TestInsta[9747:281440] [] nw_connection_receive_internal_block_invoke [C1] Receive reply failed with error "Operation canceled"

https://prnt.sc/pgzldk

sbertix commented 5 years ago

That actually looks like some sort of Xcode warning, not something SwiftyInsta generated 🤔

hassmich commented 5 years ago

I have a similar problem. It is not clear how to get into the failure block to set an authorization code from SMS. Can you help with an example?

Yagia commented 5 years ago

Hello I have same problem too. Can you share a working sample application for two factor auth ?

TheM4hd1 commented 5 years ago

@sbertix will post a clear example soon. be patient.

sbertix commented 5 years ago

Sorry I haven't managed to post an example yet, I've been really busy. I hope I can post one soon.

champion-leo commented 5 years ago

I'm totally new with swift but maybe I found the bug:

sbertix commented 5 years ago

I didn't have a chance to investigate it further but it might as well be. Apparently on the current commit Credentials are declared as a struct and not a class, which was the premise of the all the passing by reference thing.
It's apparently something I either fixed along the way in my local version, or some changes that I pushed to the master along the way, or possibly not even the issue per se. I'll try to review everything for #96, but I haven't had any time as of lately. Apologies. @Lyusan