How to setup the callback URL in the OauthSwift library

亡梦爱人 提交于 2019-12-06 06:43:39

问题


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.

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.

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

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 {

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

}


回答1:


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

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".

So instead of:

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

it was looking for:

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

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>

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.

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



来源:https://stackoverflow.com/questions/33788368/how-to-setup-the-callback-url-in-the-oauthswift-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!