1

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).

Can Poyrazoğlu
  • 33,241
  • 48
  • 191
  • 389

1 Answers1

1

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

Make sure that:

  • That your Twitter account is connected and is listed in the Settings app Twitter settings. I know you already checked, but it is essential that you verify it. Maybe even restart your phone and make sure your Twitter handle is there afterwards.
  • That your app has requested and granted permission to access the account (using the requestAccessToAccountsWithType method). Once permission is granted, you should see your app listed in the Twitter settings (in the Settings app) under "ALLOW THESE APPS TO USE YOUR ACCOUNT".
  • The Twitter account that you're setting to the SLRequest is valid and is not nil.

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]);
         }];
    }
}];
Artal
  • 8,933
  • 2
  • 27
  • 30
  • It was my bad. I don't know how I made such a simple mistake but my `self.twitterAccount` was nil. In some of my tests, it wasn't, I have no idea why it didn't work at that time. But then it was always nil. After getting a valid Twitter account object there, it worked. – Can Poyrazoğlu Jan 18 '16 at 08:37
  • Could you please add a little update to your answer regarding checking if twitterAccount is nil, which was the actual solution to my issue, so I can reward you the bounty? – Can Poyrazoğlu Jan 19 '16 at 07:07
  • @CanPoyrazoğlu done (added to the list of "things to make sure"). Thanks! :) – Artal Jan 19 '16 at 08:28