Slack Login does't work. Swift 3.0

放肆的年华 提交于 2019-12-12 03:09:27

问题


I develop iOS app with Slack integration using Swift 3.0. How I pass login process:

func loginToSlackWithSafariBrowser() {
    let scope = "channels%3Awrite+users%3Aread"
    let clientId = "...myClientId"
    let redirect_uri = "myAppDeepLink://"
    let authURL = NSURL(string: "https://slack.com/oauth/authorize?client_id=\(clientId)&scope=\(scope)&redirect_uri=\(redirect_uri)")

    guard let url  = authURL else { return }
    UIApplication.shared.openURL(url as URL)
}

Then Safari app opens, I enter credential, tap "Authorize" and have alert like "Open in your app?" I tap yes and redirecting to my app, where I send next request with gained from Slack code:

        //AppDelegate.swift
extension UIApplicationDelegate {
func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: AnyObject) -> Bool {
    self.exchangeCodeInURL(codeURL: url as NSURL)
    return true
}

func exchangeCodeInURL(codeURL : NSURL) {
    let clientId = "...myClientId"
    let clientSecret = "...myclientSecret"
    if let code = codeURL.query {
        let request = NSMutableURLRequest(url: NSURL(string: "https://slack.com/api/oauth.access?client_id=\(clientId)&client_secret=\(clientSecret)&code=\(code)") as! URL)
        request.httpMethod = "GET"
        request.setValue("application/json", forHTTPHeaderField: "Accept")
        URLSession.shared.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
            print(response)
            guard let unwrappedData = data else {return}
            do {
                if let rootObject = try JSONSerialization.jsonObject(with: unwrappedData, options: []) as? NSDictionary {
                    //Save and handle the token
                    print(rootObject)
                }
            }
            catch {
                print(error)
            }
        }).resume()
    }
}

}

Code worked in Xcode 8 beta, but when I updated to Xcode 8 functions in extension are not called after redirect from Slack website.

What is wrong? Is there a better way to pass Slack login process?


回答1:


You may need to parse code before sending it to the oauth.access GET. When I run this on XCode 8 and Swift 2.3, my code contains "code=Xxxx&state=".

I'm working on the same problem, and appreciated your question as it helped me get started with the authentication process.




回答2:


Ok, mistake is really stupid... Instead of

func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: AnyObject) -> Bool

which is deprecated

func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool

has to be implemented.

So this should be in your AppDelegate

 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool { //don't miss to implement this!
    return true
}

func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool{
    self.exchangeCodeInURL(codeURL: url)
    return true
}

func exchangeCodeInURL(codeURL : URL) {
    let clientId = "...myClientId"
    let clientSecret = "...myclientSecret"
    if let code = codeURL.query {
        guard let url = URL(string: "https://slack.com/api/oauth.access?client_id=\(clientId)&client_secret=\(clientSecret)&\(code)") else {return} //as code = "code=Xxxx&state=" you don't have to extract code from string, this query works good
        let request = NSMutableURLRequest(url: url)
        request.setValue("application/json", forHTTPHeaderField: "Accept")
        request.httpMethod = "GET"
        URLSession.shared.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
            print(response)
            guard let unwrappedData = data else {return}
            do {
                if let rootObject = try JSONSerialization.jsonObject(with: unwrappedData, options: []) as? NSDictionary {
                    //Save and handle the token
                    print(rootObject)
                }
            }
            catch {
                print(error)
            }
        }).resume()
    }
}


来源:https://stackoverflow.com/questions/39536851/slack-login-doest-work-swift-3-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!