Last active
August 29, 2015 13:58
-
-
Save pepicrft/10169623 to your computer and use it in GitHub Desktop.
Learn how to connect Redbooth API in Objective-C using Oauth2. We are going to use AFNetworking and GROAuth2SessionManager libraries to simplify the process. Learn how, and start integrating Redbooth in your apps
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
platform :ios, '7.0' | |
inhibit_all_warnings! | |
pod 'AFNetworking', '~> 2.2' | |
pod 'GROAuth2SessionManager', '~> 0.2' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (void)loginWithUser:(NSString*)username password:(NSString*)password | |
{ | |
NSURL *apiURL = [[RedboothAPIClient sharedInstance] baseURL]; | |
// API Client and Secret are required | |
// You can get them from Redbooth https://redbooth.com/oauth_clients registering your application | |
// Once got them, introduce in two constants bellow | |
NSString *apiClient = @""; | |
NSString *apiSecret = @""; | |
GROAuth2SessionManager *sessionManager = [GROAuth2SessionManager managerWithBaseURL:apiURL clientID:apiClient secret:apiSecret]; | |
[sessionManager authenticateUsingOAuthWithPath:@"/oauth/token" | |
username:username | |
password:password | |
scope:@"read_projects write_projects offline_access" | |
success:^(AFOAuthCredential *credential) { | |
NSLog(@"Login successful"); | |
//Setting token singleton Redbooth API Client | |
[[RedboothApiClient sharedInstance] setAuthorizationHeaderWithToken:credential.accessToken]; | |
// Enjoy! | |
} | |
failure:^(NSError *error) { | |
NSLog(@"Error login: %@", error); | |
}]; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// RedboothAPIClient.h | |
// Created by Redbooth | |
// | |
@interface RedboothApiClient : AFHTTPSessionManager | |
// Singleton instance | |
+ (RedboothApiClient *)sharedInstance; | |
+ (void)resetInstance; | |
// Authentication | |
- (void)setAuthorizationHeaderWithToken:(NSString *)token; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// RedboothAPIClient.h | |
// Created by Redbooth | |
// | |
#import "RedboothApiClient.h" | |
#define apiURL @"https://redbooth.com" | |
static RedboothApiClient *_sharedInstance = nil; | |
@implementation RedboothApiClient | |
#pragma mark - Singleton instance | |
+ (RedboothApiClient *)sharedInstance | |
{ | |
static dispatch_once_t onceToken; | |
dispatch_once(&onceToken, ^{ | |
_sharedInstance = [[RedboothApiClient alloc] initWithBaseURL:[NSURL URLWithString:apiURL]]; | |
}); | |
return _sharedInstance; | |
} | |
- (id)initWithBaseURL:(NSURL *)url | |
{ | |
self = [super initWithBaseURL:url]; | |
if (self) { | |
[self setRequestSerializer:[AFJSONRequestSerializer serializer]]; | |
[self setResponseSerializer:[AFJSONResponseSerializer serializer]]; | |
self.responseSerializer.acceptableContentTypes = [NSSet setWithArray:@[@"text/html",@"application/json",@"text/plain"]]; | |
} | |
return self; | |
} | |
+ (void)resetInstance | |
{ | |
[_sharedInstance clearAuthorizationHeader]; | |
[_sharedInstance updateHeader]; | |
NSHTTPCookieStorage* cookies = [NSHTTPCookieStorage sharedHTTPCookieStorage]; | |
NSArray* httpCookies = [cookies cookies]; | |
for (NSHTTPCookie* cookie in httpCookies) { | |
[cookies deleteCookie:cookie]; | |
} | |
} | |
#pragma mark - Authentication | |
- (void)setAuthorizationHeaderWithToken:(NSString *)token | |
{ | |
[self.requestSerializer setValue:[NSString stringWithFormat:@"OAuth %@", token] forHTTPHeaderField:@"Authorization"]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment