松弛登录不起作用。斯威夫特 3.0



我使用Swift 3.0开发与Slack集成的iOS应用程序。如何通过登录过程:

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)
}

然后Safari应用打开,我输入凭证,点击"授权",提示"在你的应用中打开?"我点击"是"并重定向到我的应用程序,在那里我发送下一个请求,从Slack代码获得:

        //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()
    }
}

}

代码在Xcode 8测试版中工作,但是当我更新到Xcode 8时,扩展中的函数在从Slack网站重定向后不会被调用。

怎么了?是否有更好的方式通过Slack登录流程?

您可能需要在将代码发送到auth之前对其进行解析。访问得到。当我在XCode 8和Swift 2.3上运行这个代码时,我的代码包含"code=Xxxx&state="。

我正在处理同样的问题,感谢您的问题,因为它帮助我开始了身份验证过程。

好吧,错误真的很愚蠢…而不是

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

已弃用

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

必须实现

这个应该在你的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()
    }
}

最新更新