OAuth2 client for OSX and iOS [based on http://lukeredpath.co.uk]
Objective-C C
Pull request Compare This branch is 1 commit ahead of doo:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.



this project providers an oauth2 library for iOS5 and OSX with support for Bearer token as well as the MAC Token.

Originally forked from lukedpath's oath library for iOS I expanded and modified it to work as a 'drop-in' static library for OSX 10.7 and IOS 5. I expanded the original framework to more gracefully handle errors and to be easier to use. Especially I simplified the refreshing of tokens.

It should 'fully' comply to the latest oauth RFC and implemented support for the MAC Token.

That said, I only implemented what I needed and there's also almost no documentation.

example usage

get access token

- (void)getToken {
    //prepare client
    NSString *clientID = [_clientCredentials objectForKey:OAuthClientClientId];
    NSString *clientSecret = [_clientCredentials objectForKey:OAuthClientClientSecret];
    NSURL *redirectURL = [NSURL URLWithString:OAuthClientRedirectURL];  
    _client = [[LROAuth2Client alloc] initWithClientID:clientID secret:clientSecret redirectURL:redirectURL];
    _client.delegate = self;

    //refresh a store token or get valid token if none stored
    if(_accessToken && _accessToken.hasExpired) {
        NSURL *tokenURL = [NSURL URLWithString:OAuthClientTokenURL];
        _client.tokenURL = tokenURL;

        //refresh it 
        [_client refreshAccessToken:_accessToken];          
    else if(!_accessToken) {
        NSURL *userURL = [NSURL URLWithString:OAuthClientUserURL];
        _client.userURL = userURL;

        //get a token
        NSURLRequest *r = [_client userAuthorizationRequestWithParameters:nil];
        dispatch_async(dispatch_get_main_queue(), ^{
                [[UIApplication sharedApplication] openURL:r.URL];
                [[NSWorkspace sharedWorkspace] openURL:r.URL];

//called after we presented the url and are ready to continue.
//our client's delegate will be called  after we are done here
- (void)handleAuthorizationURL:(NSURL*)url {
    NSURL *tokenURL = [NSURL URLWithString:OAuthClientTokenURL];
    _client.tokenURL = tokenURL;

    [_client extractAccessCodeFromCallbackURL:url];

authorize HTTP Request

//adds Authorization header to request 
- (void)authorizeHTTPRequest:(ASIHTTPRequest*)request {
        @throw [NSException exceptionWithName:@"LROAuth2ClientAuthorizationException" reason:@"should authorize, but have no token" userInfo:nil];

    if([_accessToken hasExpired]) {
        @throw [NSException exceptionWithName:@"LROAuth2ClientAuthorizationException" reason:@"should authorize, but needs to refresh the access token" userInfo:nil];
    [_accessToken authorizeHTTPRequest:request];