Skip to content

Instantly share code, notes, and snippets.

@mrhether
Created May 3, 2017 15:53
Show Gist options
  • Save mrhether/1707834d7e74dc72808b6727be1a2448 to your computer and use it in GitHub Desktop.
Save mrhether/1707834d7e74dc72808b6727be1a2448 to your computer and use it in GitHub Desktop.
Twitter Auth Example
#import <AFNetworking/AFNetworking.h>
- (void) requestToken {
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithSessionConfiguration:configuration];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
NSData *plainData = [[NSString stringWithFormat:@"%@:%@", @"YOUR_TOKEN", @"YOUR_SECRET"] dataUsingEncoding:NSUTF8StringEncoding];
NSString *encodedUsernameAndPassword = [plainData base64EncodedStringWithOptions:0];
NSString *authHeader = [NSString stringWithFormat:@"Basic %@", encodedUsernameAndPassword];
[manager.requestSerializer setValue:authHeader forHTTPHeaderField:@"Authorization"];
NSString *URL = @"https://api.twitter.com/oauth2/token?grant_type=client_credentials";
[manager POST:URL parameters:nil progress:nil
success:^(NSURLSessionDataTask *operation, id responseObject) {
}
failure:^(NSURLSessionDataTask *operation, NSError *error) {
}];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment