How to setup the callback URL in the OauthSwift li

2019-04-12 12:14发布

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条回答
孤傲高冷的网名
2楼-- · 2019-04-12 13:08

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.

查看更多
登录 后发表回答