How to support OAuth in ASIHTTPRequest

ASIHTTPRequest is a brilliant library, when you want to handle HTTP requests.
At this time, however, this library doesn’t support OAuth.

You can support it easily with OAuthCore library which is developed by atebits who is the developer of Tweetie a.k.a. Twitter for Mac/iPhone.
atebits / OAuthCore / overview – Bitbucket

A sample code is following.

#import "OAuthCore.h"

NSString * const CONSUMER_KEY = @"YOUR_CONSUMER_KEY";
NSString * const CONSUMER_SECRET = @"YOUR_CONSUMER_SECRET";
NSString * const ACCESS_TOKEN = @"YOUR_ACCESS_TOKEN";
NSString * const ACCESS_TOKEN_SECRET = @"YOUR_ACCESS_TOKEN_SECRET";

NSURL *URL = [NSURL URLWithString:@"http://example.com/api"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:URL];

[request setPostValue:@"test" forKey:@"test_value"];
[request buildPostBody];

NSString *header = OAuthorizationHeader([request url],
                                        [request requestMethod],
                                        [request postBody],
                                        CONSUMER_KEY,
                                        CONSUMER_SECRET,
                                        ACCESS_TOKEN,
                                        ACCESS_TOKEN_SECRET);


[request addRequestHeader:@"Authorization" value:header];

If you want to get “Access Token” and “Access Toke Secret”, such as you authorize with xAuth, you should set both ACCESS_TOKEN and ACCESS_TOKEN_SECRET, not nil but @””.

References