How to setup the callback URL in the OauthSwift library
Asked Answered
O

1

5

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

}

Olimpiaolin answered 18/11, 2015 at 19:11 Comment(1)
Have you implemented the application:openURL... method in your app delegate?Edvard
O
8

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.

Olimpiaolin answered 19/11, 2015 at 19:44 Comment(2)
Right now you must enter an absolute URI that starts with http:// or https:// into instagram developer page, do you have a solution for that?Hydrotherapy
Thank you so much! Was stuck for a while. Also wanted to add that openURL might not be fired every single time a url opens the app (at least it didn’t in my case, so I had to create a WKWebView to intercept the Callback.Syndesmosis

© 2022 - 2024 — McMap. All rights reserved.