我使用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应用程序打开,我输入凭证,点击"授权"和#34;在您的应用程序中打开?" 我点击是并重定向到我的应用程序,我从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 beta中工作,但是当我更新到Xcode 8时,在从Slack网站重定向后没有调用扩展中的函数。
有什么问题? 有没有更好的方法来通过Slack登录过程?
答案 0 :(得分:1)
您可能需要在将代码发送到oauth.access GET之前解析代码。当我在XCode 8和Swift 2.3上运行时,我的代码包含" code = Xxxx& state ="。
我正在处理同样的问题,并感谢您的问题,因为它帮助我开始了身份验证过程。
答案 1 :(得分:0)
好的,错误真的很愚蠢......而不是
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()
}
}