I am trying to get around this OAuth issue of getting an access token from Instapaper, it says in their site that they don't there is no request-token/authorize workflow and they said this is pretty similar to what twitter does, so here's what I did:
- (void)getXAuthAccessToken
{
OAConsumer *consumer = [[[OAConsumer alloc] initWithKey:CONSUMER_KEY secret:CONSUMER_SECRET] autorelease];
OAMutableURLRequest *request = [[OAMutableURLRequest alloc] initWithURL:[NSURL URLWithString:INSTAPAPER_CLIENT_OAUTH_URL]
consumer:consumer
token:nil // xAuth needs no request token?
realm:nil // our service provider doesn't specify a realm
signatureProvider:nil]; // use the default method, HMAC-SHA1
[request setHTTPMethod:@"POST"];
[request setParameters:[NSArray arrayWithObjects:
[OARequestParameter requestParameterWithName:@"x_auth_mode" value:@"client_auth"],
[OARequestParameter requestParameterWithName:@"x_auth_username" value:self.username],
[OARequestParameter requestParameterWithName:@"x_auth_password" value:self.password],
nil]];
[request prepare];
AFHTTPRequestOperation *requestOAuth = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[requestOAuth setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject){
NSLog(@"RESPONSE OBJECT IS %@", responseObject);
}failure:^(AFHTTPRequestOperation *operation, NSError *error){
NSLog(@"ERROR IS %@", error);
}];
[requestOAuth start];
}
Any idea why this is constantly giving me a 401?
So turns out I'd have to call prepare on this request first. But then I can't read the responseData back as what I see it now just binaries.. how can I convert this to a human readable?
EDIT:
Set parameter puts the params in the header:
- (void)setParameters:(NSArray *)parameters
{
NSMutableString *encodedParameterPairs = [NSMutableString stringWithCapacity:256];
int position = 1;
for (OARequestParameter *requestParameter in parameters)
{
[encodedParameterPairs appendString:[requestParameter URLEncodedNameValuePair]];
if (position < [parameters count])
[encodedParameterPairs appendString:@"&"];
position++;
}
if ([[self HTTPMethod] isEqualToString:@"GET"] || [[self HTTPMethod] isEqualToString:@"DELETE"])
[self setURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?%@", [[self URL] URLStringWithoutQuery], encodedParameterPairs]]];
else
{
// POST, PUT
NSData *postData = [encodedParameterPairs dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
[self setHTTPBody:postData];
[self setValue:[NSString stringWithFormat:@"%d", [postData length]] forHTTPHeaderField:@"Content-Length"];
[self setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
}
}