如何在OauthSwift库中设置回调URL
我正在开发一个项目,我正在实施OAuthSwift库,以连接到使用OAuth1和OAuth2的几个不同的社交网站。
我将应用程序设置为加载一个Web视图,将我带到我的社交网站,但我无法让应用程序重定向回来。 我加载凭据后,它会要求我授予对应用程序进行授权的权限,但是一旦我这样做,就会加载我的社交网站主页。
我可以导航回应用程序,但它没有注册它已获得访问我的帐户的权限。
这是我第一次使用OAuth,我发现回调url令人困惑。
我将非常感谢帮助解释如何让Web视图重定向回我的应用程序以及如何设置应用程序的URL。
class ViewController:UIViewController {
override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } @IBAction func postToTumblr(sender: AnyObject) { let oauthSwift = OAuth1Swift( consumerKey: "consumerKey", consumerSecret: "secretKey", requestTokenUrl: "https://www.tumblr.com/oauth/request_token", authorizeUrl: "https://www.tumblr.com/oauth/authorize", accessTokenUrl: "https://www.tumblr.com/oauth/access_token" ) oauthSwift.authorizeWithCallbackURL(NSURL(string: "com.myCompany.sampleApp")!, success: { credential, response in // post to Tumblr print("OAuth successfully authorized") }, failure: {(error:NSError!) -> Void in self.presentAlert("Error", message: error!.localizedDescription) }) } func presentAlert(title: String, message: String) { let alert = UIAlertController(title: title, message: message, preferredStyle: .Alert) alert.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil)) self.presentViewController(alert, animated: true, completion: nil) }
}
在与我公司的一些人交谈并让他们查看图书馆后,我们能够按如下方式解决问题:
OAuthSwift库删除了URL方案的“com.myCompany”部分。 当它在寻找回调URL时,它正在查找应用程序的名称,后跟“:// oauth-callback”。
所以代替:
oauthSwift.authorizeWithCallbackURL(NSURL(string: "com.myCompany.sampleApp")!
它正在寻找:
oauthSwift.authorizeWithCallbackURL(NSURL(string: "tumblrsampleapp://oauth-callback")!
我还必须在info.plist中注册URL方案:
CFBundleURLTypes CFBundleURLSchemes tumblrsampleapp
最后,我必须将以下方法添加到App Delegate:
func application(app: UIApplication, openURL url: NSURL, options: [String : AnyObject]) -> Bool { OAuth1Swift.handleOpenURL(url) return true }
这解决了问题,现在应用程序正确validation并返回到我的应用程序。
我希望这对尝试使用OAuthSwift库实现OAuth1的其他人有用。
- 如何使用代码连接dataSource和delegate – iOS Swift
- Objective-C – 检查URL是否存在
- uiscrollview不会取消uisegmentedcontrol的内容
- UITextView中的断言失败_firstBaselineOffsetFromTop
- 在NSDocumentDirectory上禁用数据备份时CFURLSetResourcePropertyForKey失败
- 首次安装应用程序时存储变量
- 有没有办法在WKWebView中设置本地存储
- UINavigationController:在每次转换后显示embedded的视图控制器不同的方向?
- CLLocation当前位置不起作用