iostwitteracaccountslrequest

Updating Twitter profile picture with iOS SLRequest results in error 215 bad authentication data


I am trying to post new profile picture according to Twitter's documentation at https://dev.twitter.com/rest/reference/post/account/update_profile_image and here is my code:

NSData *jpeg = UIImageJPEGRepresentation(img, 0.9);
NSString *base64 = [jpeg base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
NSURL *url = [NSURL URLWithString:@"https://api.twitter.com/1.1/account/update_profile_image.json"];
NSDictionary *params = @{@"image" : base64
                         };
SLRequest *request =
[SLRequest requestForServiceType:SLServiceTypeTwitter
                   requestMethod:SLRequestMethodPOST
                             URL:url
                      parameters:params];
[request setAccount:self.twitterAccount];
[request performRequestWithHandler:^(NSData *responseData,
                                     NSHTTPURLResponse *urlResponse,
                                     NSError *error) {
    ...
}

But URL response is:

errors =     (
            {
        code = 215;
        message = "Bad Authentication data.";
    }
);

What am I doing wrong? My ACAccount is valid (I've just tried signing out from and into Twitter in Settings.app).


Solution

  • You code seems fine, but you must be missing something.

    Make sure that:

    Given the conditions above I was abled to modify my profile image successfully just now. Here's a sample code based on the code you've provided:

    ACAccountStore *accountStore = [[ACAccountStore alloc] init];
    ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
    [accountStore requestAccessToAccountsWithType:accountType options:nil completion:^(BOOL granted, NSError *error)
    {
        if (granted)
        {
            NSArray *accounts = [accountStore accountsWithAccountType:accountType];
            ACAccount *twitterAccount = [accounts lastObject];
    
            NSData *jpeg = UIImageJPEGRepresentation([UIImage imageNamed:@"photo.jpg"], 0.9);
            NSString *base64 = [jpeg base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
            SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeTwitter
                                                    requestMethod:SLRequestMethodPOST
                                                              URL:[NSURL URLWithString:@"https://api.twitter.com/1.1/account/update_profile_image.json"]
                                                       parameters:@{@"image" : base64}];
            [request setAccount:twitterAccount];
            [request performRequestWithHandler:^(NSData *responseData,
                                                 NSHTTPURLResponse *urlResponse,
                                                 NSError *error)
             {
                 NSLog(@"%@", [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:nil]);
             }];
        }
    }];