litebrite
litebrite

Reputation: 175

How to listen for an OAuth2 redirect uri for native Cocoa application

I'm starting my first foray into making a mac application and the first order of business is authenticating my app using OAuth with Soundcloud. Sending the authorization request is simple, but where I'm stuck is how to listen for the redirect back from Soundcloud.

I've set up my URL scheme in info.plist and set my application to listen for incoming requests, but when I try to register I don't get a response. I've tested the URL with safari and it worked fine.

Here's a code snippets from my app delegate:

- (void) applicationDidFinishLaunching:(NSNotification *)aNotification
{
NSAppleEventManager *appleEventManager = [NSAppleEventManager sharedAppleEventManager];
[appleEventManager setEventHandler:self
                       andSelector:@selector(handleGetURLEvent:withReplyEvent:)
                     forEventClass:kInternetEventClass andEventID:kAEGetURL];

[[NXOAuth2AccountStore sharedStore] setClientID:@"myClientID" 
                                         secret:@"superSecretSecret"
                               authorizationURL:[NSURL URLWithString:@"https://soundcloud.com/connect"]
                                       tokenURL:[NSURL URLWithString:@"https://api.soundcloud.com/oauth2/token"]
                                    redirectURL:[NSURL URLWithString:@"myApp://connect"]
                                 forAccountType:@"iHopeThisWorks"];

}

- (void)handleGetURLEvent:(NSAppleEventDescriptor *)event withReplyEvent:(NSAppleEventDescriptor *)replyEvent
{
NSURL *url = [NSURL URLWithString:[[event paramDescriptorForKeyword:keyDirectObject] stringValue]];
}

My guess is that soundcloud doesn't know where to find myApp://connect since it only exists on my local machine. So do I need to create some intermediary like a web service that will handle http requests?. And if so, how does the intermediary know how to interface back to my machine? Sorry if these are basic or misguided questions, but like I said earlier, this is my first shot at something like this and my google-fu has failed me in finding a solution.

Upvotes: 0

Views: 1251

Answers (2)

Artem Oboturov
Artem Oboturov

Reputation: 4385

Normally OAuth service providers should implement Resource Owner Password Credentials Grant for desktops - in your case they didn't.

But they already did a wrapper over the OAuth2Client - look for it on https://github.com/soundcloud/CocoaSoundCloudAPI. The docs are at http://developers.soundcloud.com/docs#authentication.

Answering your question the URI is myApp://connect if your application is called literally myApp. See video from developers on vimeo: http://vimeo.com/28715664.

Best and read docs.

Upvotes: 3

bijan
bijan

Reputation: 1685

You can use a NSWebView for that. I know this doesn't sound quite elegant, since you do not actually need a "view", but as far as i know this is the easiest solution for getting redirects.

Upvotes: 0

Related Questions