Search code examples
iosobjective-capiafnetworkinginstapaper

When using the Instapaper API, why am I continually getting 401 errors?


I'm trying to use the full Instapaper API via the AFOAuth2Client library but I keep getting the error code 401. I have no idea what's wrong with my code. I definitely ahve the right ID and secret as I copy and pasted them from the email.

- (IBAction)loginPressed:(UIButton *)sender {
    NSURL *baseURL = [NSURL URLWithString:@"https://www.instapaper.com/"];

    AFOAuth2Client *OAuthClient = [AFOAuth2Client clientWithBaseURL:baseURL
                                                  clientID:@"fromEmail"
                                                  secret:@"fromEmail"];

    NSDictionary *parameters = @{
        @"x_auth_username:" : self.usernameField.text,
        @"x_auth_password:" : self.passwordField.text,
        @"x_auth_mode:" : @"client_auth"
    };

    [OAuthClient authenticateUsingOAuthWithPath:@"api/1/oauth/access_token"
                 parameters:parameters
                 success:^(AFOAuthCredential *credential) {
                     NSLog(@"I has token! %@", credential.accessToken);
                     // [AFOAuthCredential storeCredential:credential withIdentifier:OAuthClient.serviceProviderIdentifier];
                 }
                 failure:^(NSError *error) {
                     NSLog(@"Sheet. %@", [error localizedDescription]);
                 }];
}

Solution

  • According to Instapaper's API docs:

    Instapaper’s OAuth implementation is different from what you may be accustomed to: there is no request-token/authorize workflow. This makes it much simpler. Instead, Instapaper uses an implementation of xAuth very similar to Twitter’s. So you still need to sign your requests, but getting tokens is simple.

    xAuth is the only way to get an Instapaper access token.

    OAuth 2 is different from OAuth 1, which is itself different from xAuth.

    AFOAuth2Client is not going to work here. You may have luck with either AFXAuthClient or AFOAuth1Client.