如何在OauthSwift库中设置回调URL [英] How to setup the callback URL in the OauthSwift library

查看:103
本文介绍了如何在OauthSwift库中设置回调URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开展一个项目,我正在实施OAuthSwift库,以连接到使用OAuth1和OAuth2的几个不同的社交网站。

I am working on a project where I am implementing the OAuthSwift library to connect to several different social networking sites that use both OAuth1 and OAuth2.

我有应用程序设置为加载一个Web视图,将我带到我的社交网站,但我无法让应用程序重定向回来。一旦我加载了我的凭据,它就会要求我授权该应用程序,但是一旦我这样做,就会加载我的社交网站主页。

I have the application set up to load a web view that takes me to my social networking site, but I can't get the application to redirect back. Once I load my credentials, it asks for me to give permission to authorize the application, but once I do, it loads my homepage for the social networking site.

我可以导航回应用程序,但它没有注册它已获得访问我的帐户的权限。

I can navigate back to the application, but it does not register that it has received permission to access my account.

这是我第一次使用OAuth,我找到了回调网址令人困惑。

This is my first time working with OAuth and I find the callback URL to be confusing.

我很感激帮助解释如何让网页视图重定向回我的应用程序以及如何设置应用程序的URL。

I would appreciate some help explaining how to get the web view to redirect back to my application and how to set up the URL for the app.

class ViewController:UIViewController {

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

}

推荐答案

与某些人交谈后我公司的人员让他们查看图书馆,我们能够按如下方式解决问题:

After speaking with some people at my company and having them look over the library, we were able to resolve the issue as follows:

OAuthSwift库删除了com.myCompany部分网址方案。当它在寻找回调URL时,它正在寻找应用程序的名称,后跟:// oauth-callback。

The OAuthSwift library drops the "com.myCompany" part of the URL scheme. When it is looking for the callback URL, it is looking for the name of the application followed by "://oauth-callback".

所以代替:

oauthSwift.authorizeWithCallbackURL(NSURL(string: "com.myCompany.sampleApp")!

它正在寻找:

oauthSwift.authorizeWithCallbackURL(NSURL(string: "tumblrsampleapp://oauth-callback")!

我还必须注册info.plist中的URL方案为:

I also had to register the URL scheme in the info.plist as:

<key>CFBundleURLTypes</key>
<array>
    <dict>
        <key>CFBundleURLSchemes</key>
        <array>
            <string>tumblrsampleapp</string>
        </array>
    </dict>
</array>

最后,我必须将以下方法添加到App Delegate:

Lastly, I had to add the following method to the App Delegate:

func application(app: UIApplication, openURL url: NSURL, options: [String : AnyObject]) -> Bool {
    OAuth1Swift.handleOpenURL(url)
    return true
}

这解决了问题,应用程序现在正确验证并返回我的应用程序。

This has resolved the issue and the app now authenticates properly and returns back to my application.

我希望这对尝试使用OAuthSwift实现OAuth1的其他人有用图书馆。

I hope this is useful to anyone else trying to implement OAuth1 using the OAuthSwift library.

这篇关于如何在OauthSwift库中设置回调URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆