nerdishbynature / octokit.swift

A Swift API Client for GitHub and GitHub Enterprise
MIT License
490 stars 126 forks source link

OAuth issue #63

Closed muhdmirzamz closed 6 years ago

muhdmirzamz commented 7 years ago

Hi,

Big fan of this project. One problem though, I have tried the OAuth config settings and gotten the code attribute. I followed the code example you provided in the README.md but printing out user.login does not work. Mind helping me out here?

pietbrauer commented 7 years ago

Hey, that's strange. Could you provide me with some code that you use?

muhdmirzamz commented 7 years ago

Thanks for getting back and sorry for the late reply!

Here's my code. Do note this follows the Swift 3 syntax so some of the documentation code is a little different here.

My main view controller

    var config = (UIApplication.shared.delegate as! AppDelegate).config

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.

        var clientId = "<client_id>"
        var clientSecret = "<client_secret>"

        self.config = OAuthConfiguration.init(token: clientId, secret: clientSecret, scopes: ["repo", "read:org"])
        let url = config?.authenticate()
        UIApplication.shared.open(url!, options: [:], completionHandler: nil)
    }

My AppDelegate

        var config: OAuthConfiguration?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        return true
    }

    func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
        self.config?.handleOpenURL(url: url, completion: { (token) in
            Octokit(token).me() { response in
                switch response {
                case .success(let user):
                    print("User login: \(user.login)")
                case .failure(let error):
                    print("Error: \(error)")
                }
            }
        })

        return false
    }
muhdmirzamz commented 6 years ago

Hey, just an FYI. I managed to figure out the issue. I just put the code from my main view controller to my appdelegate's

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

}

function. All good now!

pietbrauer commented 6 years ago

Alright, glad you solved it. Sorry I didn't have the time to investigate.

muhdmirzamz commented 6 years ago

Would you like me to provide an updated example code snippet for Swift 3 or update the existing README or documentations?

pietbrauer commented 6 years ago

Yes please, that would be very helpful.

muhdmirzamz commented 6 years ago

Okay, it's done in pull request #64 👍