1

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"];
    }
}
aherlambang
  • 14,290
  • 50
  • 150
  • 253

1 Answers1

0

After reading the Instapaper API docs, it looks like you need to provide a token in the request.

the page is here: http://www.instapaper.com/main/request_oauth_consumer_token

nebulae
  • 2,665
  • 1
  • 19
  • 16
  • I did register on that site and they emailed me back with a consumer key and secret which I provided in the request above – aherlambang Jul 27 '12 at 22:52
  • It also states that the parameters should be passed in the Authorization: header, not the request parameters. There's a thread here that might be of help: http://stackoverflow.com/questions/1973325/nsurlconnection-and-basic-http-authentication – nebulae Jul 27 '12 at 22:57