AFOAuth2Client is an extension for AFNetworking that greatly simplifies the process of authenticating against an OAuth 2 provider.

Example Usage

Authentication

NSURL *baseURL = [NSURL URLWithString:@"http://example.com/"];
AFOAuth2Client *OAuth2Client = [AFOAuth2Client clientWithBaseURL:baseURL
                                                        clientID:kClientID
                                                          secret:kClientSecret];

[OAuth2Client authenticateUsingOAuthWithPath:@"/oauth/token"
                                    username:@"username"
                                    password:@"password"
                                       scope:@"email"
                                     success:^(AFOAuthCredential *credential) {
                                         NSLog(@"Token: %@", credential.accessToken);
                                     }
                                     failure:^(NSError *error) {
                                         NSLog(@"Error: %@", error);
                                     }];

Authorizing Requests

AFHTTPClient *client = [AFHTTPClient clientWithBaseURL:baseURL];
[client setDefaultHeader:@"Authorization"
                   value:[NSString stringWithFormat:@"Bearer %@", credential.accessToken]];


[client getPath:@"/path/to/protected/resource"
     parameters:nil
        success:^(AFHTTPRequestOperation *operation, id responseObject) {
            NSLog(@"Success: %@", responseObject);
        }
        failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"Failure: %@", error);
        }];

Storing Credentials

[AFOAuthCredential storeCredential:credential
                    withIdentifier:serviceProviderIdentifier];

Retrieving Credentials

AFOAuthCredential *credential =
        [AFOAuthCredential retrieveCredentialWithIdentifier:serviceProviderIdentifier];

Documentation

Documentation for all releases of AFOAuth2Client are available on CocoaDocs.

Contact

Mattt Thompson

License

AFOAuth2Client is available under the MIT license. See the LICENSE file for more info.