【问题标题】:SafariViewController: How to grab OAuth token from URL?SafariViewController:如何从 URL 中获取 OAuth 令牌?
【发布时间】:2016-12-13 14:43:36
【问题描述】:

尝试将 Facebook OAuth 与 SafariViewController 一起使用。首先,我使用 SafariViewController 打开 authURL,如果用户在 Safari 上登录到 Facebook,它将重定向他们并返回带有特定服务令牌的 OAuth URL,例如Instagram

回复:https://www.facebook.com/connect/login_success.html#access_token=BLAHTOKENRESPONSE&expires_in=5114338

当 SafariViewController 重定向后,我想获取响应 URL 并将其存储,以便获取令牌。这是我的代码:

import SafariServices

let kSafariViewControllerCloseNotification = "kSafariViewControllerCloseNotification"

import UIKit

// facebook OAuth URL for service
let authURL = NSURL(string: "https://www.facebook.com/dialog/oauth?client_id=3627644767&redirect_uri=https://www.facebook.com/connect/login_success.html&scope=basic_info,email,public_profile,user_about_me,user_activities,user_birthday,user_education_history,user_friends,user_interests,user_likes,user_location,user_photos,user_relationship_details&response_type=token")

class ViewController: UIViewController, SFSafariViewControllerDelegate {

    var safariVC: SFSafariViewController?
    @IBOutlet weak var loginButton: UIButton!

    @IBAction func loginButtonTapped(sender: UIButton) {
        safariVC = SFSafariViewController(URL: authURL!)
        safariVC!.delegate = self
        self.presentViewController(safariVC!, animated: true, completion: nil)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // not firing the safariLogin function below
        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ViewController.safariLogin(_:)), name: kSafariViewControllerCloseNotification, object: nil)
    }

    func safariLogin(notification: NSNotification) {
        print("Safari Login call")
        // get the url form the auth callback
        let url = notification.object as! NSURL
        print(url)
        self.safariVC!.dismissViewControllerAnimated(true, completion: nil)
    }

    func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool {
        print("application call")
        // just making sure we send the notification when the URL is opened in SFSafariViewController
        if (sourceApplication == "com.application.SafariViewTest") {
            NSNotificationCenter.defaultCenter().postNotificationName(kSafariViewControllerCloseNotification, object: url)
            return true
        }
        return true
    }  

}

它会打开 authURL 并重定向到正确的响应 URL,但观察者不会触发 safariLogin 函数来获取 URL。任何帮助将不胜感激!

非常感谢!

【问题讨论】:

    标签: swift facebook oauth swift2 sfsafariviewcontroller


    【解决方案1】:

    iOS 12+

    iOS 12 Beta 已弃用 SFAuthenticationSession(见下文)以支持 ASWebAuthenticationSession。看起来使用方式完全相同,但需要新的AuthenticationServices 框架。

    iOS 11

    iOS 11 引入了SFAuthenticationSession,它更容易处理。鉴于其性质,此 beta API 可能仍会发生变化,但互联网上已经有几个示例(12)。首先,您需要一个使用身份验证请求的结果调用的完成处理程序:

    let completion : SFAuthenticationSession.CompletionHandler = { (callBack:URL?, error:Error?) in
        guard error == nil, let successURL = callBack else {
            return
        }
    
        let oauthToken = NSURLComponents(string: (successURL.absoluteString))?.queryItems?.filter({$0.name == "oauth_token"}).first
    
        // Do what you have to do...
    }
    

    然后您只需创建一个 SFAuthenticationSession 并启动它。

    let authURL = "https://the.service.you/want/toAuthorizeWith?..."
    let scheme = "YOURSCHEME://"
    let authSession = SFAuthenticationSession(url: authURL, callbackURLScheme: scheme, completionHandler: completion)
    authSession.start()
    

    iOS 10 及之前的版本

    正如一些人在 cmets 中指出的那样,接受的答案是不完整的,并且不会单独工作。在浏览草莓代码的博客文章时,可以找到link to the related GitHub project。该项目的README.MD 解释了设置的关键部分,即将重定向URI 添加到Info.plist。所以整个事情是这样的:

    AppDelegate.swift

    func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
    
       if let sourceApplication = options[.sourceApplication] {
           if (String(describing: sourceApplication) == "com.apple.SafariViewService") {
                NotificationCenter.default.post(name: Notification.Name("CallbackNotification"), object: url)
                return true
            }
        }
    
        return false
    }
    

    ViewController.swift

    注册通知以在某个合理的地方调用您的处理程序。我建议不要在 viewDidLoad() 中这样做,但只能在您实际展示 SFSafariViewController 之前这样做。

    NotificationCenter.default.addObserver(self, selector: #selector(safariLogin(_:)), name: Notification.Name("CallbackNotification"), object: nil)
    let safariVC = SFSafariViewController(URL: authURL)
    safariVC.delegate = self
    self.present(safariVC, animated: true, completion: nil)
    

    然后删除处理程序中的遵守:

    @objc func safariLogin(_ notification : Notification) {
    
        NotificationCenter.default.removeObserver(self, name: Notification.Name("CallbackNotification"), object: nil)
    
        guard let url = notification.object as? URL else {
            return
        }
    
        // Parse url ...
    
    }
    

    请记住,用户可以通过点击Done 按钮来关闭SFSafariViewController,因此请务必采用SFSafariViewControllerDelegate 协议并删除这样的遵守:

    func safariViewControllerDidFinish(_ controller: SFSafariViewController) {
        NotificationCenter.default.removeObserver(self, name: Notification.Name("CallbackNotification"), object: nil)
    }
    

    Info.plist

    要使这一切正常工作,您需要将重定向 URI 方案添加到 Info.plist

    <key>CFBundleURLTypes</key>
    <array>
        <dict>
            <key>CFBundleURLName</key>
            <string>com.YOUR.BUNDLE.IDENTIFIER</string>
            <key>CFBundleURLSchemes</key>
            <array>
                <string>YOURSCHEME</string>
            </array>
        </dict>
    </array>
    

    当然 YOURSCHEME 必须与您在尝试授权的 Web 服务中注册的重定向 URI 的方案相匹配。

    【讨论】:

    • 嘿,我确实喜欢您为 iOS 10 及更低版本编写的内容,包括添加方案。但是由于某种原因,AppDelegate 的方法没有被调用。你知道为什么吗?我的架构看起来像https://example.com 重定向导致https://example.com/redirect。从我的角度来看,这应该可行,对吧?
    • @VyachaslavGerchicov 你有没有找到解决这个问题的方法?我也面临同样的问题。
    • @bfx:要自动关闭身份验证网络,应该从服务器发出什么样的 JSON 响应或操作?
    【解决方案2】:

    想通了。其中一些方法是 iOS 9 之前的版本,现在已弃用。我在创建的 ViewController 中也有 application 函数,而它应该在 AppDelagate.swift 中定义。例如

    添加在 AppDelegate.swift 的末尾

    func application(app: UIApplication, openURL url: NSURL, options: [String : AnyObject]) -> Bool {
    
            print("app: \(app)")
            // print OAuth response URL
            print("url: \(url)")
            print("options: \(options)")
    
            if let sourceApplication = options["UIApplicationOpenURLOptionsSourceApplicationKey"] {
                if (String(sourceApplication) == "com.testApp.Incognito") {
                    NSNotificationCenter.defaultCenter().postNotificationName(kSafariViewControllerCloseNotification, object: url)
                    return true
                }
            }
            return true
        }
    

    ViewController.swift

    import SafariServices
    
    let kSafariViewControllerCloseNotification = "kSafariViewControllerCloseNotification"
    
    // facebook OAuth URL
    let authURL = NSURL(string: "https://www.facebook.com/dialog/oauth?client_id=3627644767&redirect_uri=https://www.facebook.com/connect/login_success.html&scope=basic_info,email,public_profile,user_about_me,user_activities,user_birthday,user_education_history,user_friends,user_interests,user_likes,user_location,user_photos,user_relationship_details&response_type=token")
    
    class ViewController: UIViewController, SFSafariViewControllerDelegate {
    
        var safariVC: SFSafariViewController?
        @IBOutlet weak var loginButton: UIButton!
    
        @IBAction func loginButtonTapped(sender: AnyObject) {
            safariVC = SFSafariViewController(URL: authURL!)
            safariVC!.delegate = self
            self.presentViewController(safariVC!, animated: true, completion: nil)
        }
    
        override func viewDidLoad() {
            super.viewDidLoad()
            NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ViewController.safariLogin(_:)), name: kSafariViewControllerCloseNotification, object: nil)
        }
    
        func safariLogin(notification: NSNotification) {
            // get the url from the auth callback
            let url = notification.object as! NSURL
            // Finally dismiss the Safari View Controller with:
            self.safariVC!.dismissViewControllerAnimated(true, completion: nil)
        } 
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
        }
    
        func safariViewControllerDidFinish(controller: SFSafariViewController) {
            controller.dismissViewControllerAnimated(true) { () -> Void in
                print("You just dismissed the login view.")
            }
        }
    
        func safariViewController(controller: SFSafariViewController, didCompleteInitialLoad didLoadSuccessfully: Bool) {
            print("didLoadSuccessfully: \(didLoadSuccessfully)")
    
        }
    }
    

    【讨论】:

    • 我有点不明白你是如何获得重定向 url 来调用应用程序函数的?我想做类似的事情并复制你所做的,但我无法让 SFSafariViewController 回调应用程序函数
    • 是的,我也是。我想知道,重定向后 SFSafariViewController 如何回调应用程序。
    • 嗯...您只是从strawberrycode.com/blog/… 获取代码而没有对其进行认证...
    猜你喜欢
    • 2020-06-24
    • 2018-09-05
    • 1970-01-01
    • 1970-01-01
    • 2021-09-07
    • 1970-01-01
    • 1970-01-01
    • 2019-03-13
    • 2019-10-17
    相关资源
    最近更新 更多