Slacklogin不起作用。 Swift 3.0

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

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应用程序打开,我input凭据,点击“授权”,并有像“打开你的应用程序? 我点击是,并redirect到我的应用程序,在那里我发送下一个请求从获得的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 8testing版中,但是当我更新到Xcode 8扩展function后,从Slack网站redirect后不会调用。

哪里不对? 有更好的方式来传递Slacklogin过程吗?

在将代码发送到oauth.access GET之前,您可能需要parsing代码。 当我在XCode 8和Swift 2.3上运行这个时,我的代码包含“code = Xxxx&state =”。

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

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

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