4

I have application with upload photo like in Instagram app. First my app uploads photo and returns link. Then sends link in status message. When I'm testing on my iPhone everything is ok, but when I'm testing on another iPhone then twitter return code 215 bad authentication data. Any ideas what could be wrong? Thx for reply.

- (void)tweet:(NSString *)identifier withCompletionHandler:(void (^)())completionHandler {
ACAccountStore *accountStore = [[ACAccountStore alloc] init];

// Create an account type that ensures Twitter accounts are retrieved.
ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];

// Request access from the user to use their Twitter accounts.
[accountStore requestAccessToAccountsWithType:accountType withCompletionHandler:^(BOOL granted, NSError *error) {
    if(granted) {
        if (identifier) {
            // Get the list of Twitter accounts.
            NSArray *accountsArray = [accountStore accountsWithAccountType:accountType];

            if ([accountsArray count] > 0) {
                // Grab the initial Twitter account to tweet from.
                NSURL *url = [NSURL URLWithString:@"https://api.twitter.com/1.1/statuses/update.json"];
                NSDictionary *dict = [NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"Check out this great flick on BLA BLA %@", [BLA_URL stringByAppendingFormat:@"/%@/%@", @"flick", identifier]]
                                                                 forKey:@"status"];

                SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeTwitter
                                                        requestMethod:SLRequestMethodPOST
                                                                  URL:url
                                                           parameters:dict];
                [request setAccount:[accountsArray lastObject]];
                [request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
                    if ([urlResponse statusCode] == 200) {
                        NSLog(@"TWEEET!");

                    }
                    else { 
                        NSError *jsonError;
                        id data = [NSJSONSerialization JSONObjectWithData:responseData
                                                                  options:NSJSONReadingMutableLeaves
                                                                    error:&jsonError];

                        dispatch_async(dispatch_get_main_queue(), ^{
                            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Twitter response"
                                                                                message:[NSString stringWithFormat:@"%d S: %d %@", [error code], [urlResponse statusCode], data]
                                                                               delegate:self
                                                                      cancelButtonTitle:@"Ok"
                                                                      otherButtonTitles:nil];
                            NSLog(@"NOOO TWEEET!");
                            [alertView show];
                            completionHandler();
                        });
                    }
                }];
...

And Pre-checking function:

- (IBAction)twitterButtonTapped:(id)sender {
if ([self.twitterButton isSelected]) {
    [self.twitterButton setSelected:NO];
}
else {
    [self.twitterButton setSelected:YES];

    ACAccountStore *accountStore = [[ACAccountStore alloc] init];
    ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];

    [accountStore requestAccessToAccountsWithType:accountType withCompletionHandler:^(BOOL granted, NSError *error) {
        if (!granted) {
            [self.twitterButton setSelected:NO];

            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Twitter access"
                                                                message:@"Please make sure you have allowed twitter for BLA BLA in your Settings."
                                                               delegate:self
                                                      cancelButtonTitle:@"Ok"
                                                      otherButtonTitles:nil];
            [alertView performSelectorOnMainThread:@selector(show) withObject:nil waitUntilDone:NO];
        }
    }];
}

}

Michal Jurník
  • 820
  • 1
  • 8
  • 23
  • 1
    I'm having the same problem. Similar code worked until I had to switch to API v 1.1 – Jorsh Sep 30 '13 at 05:50
  • 1
    Does the other iPhone have a Twitter Account set up in the settings? Does the dialogue show up where it asks you to grant access? – äymm Sep 30 '13 at 12:48
  • Yes other iPhone has about 10 Twitter accounts. And yes, dialogue shows up when has not access. I wrote pre-check code when someone taps on "Share on twitter" button. I just added this code. – Michal Jurník Sep 30 '13 at 13:35
  • 1
    Hm, I've had the same error, but realized my code was called _before_ accountStore got initialized. This is obviously not your problem here. Maybe set a breakpoint at performRequestWithHandler and check the content of your variables? – äymm Sep 30 '13 at 13:46
  • I've checked everything but without success. – Michal Jurník Oct 04 '13 at 09:22

1 Answers1

1

Ok, I've got the answer! Problem was that other iPhone has incomplete account data - meanings you have to has filled username and password in your Settings app.

Michal Jurník
  • 820
  • 1
  • 8
  • 23
  • 1
    To be clear, for anyone with same problem, it is not enough to have signed in to Twitter app. You must also enter your account info and password in the iPhone Settings app. Fixed my failure to post. I feel this was not the case in iOS6, seems signing into Twitter app was good enough before. No way to confirm that however as all my devices have been upgraded. – Dean Davids Oct 10 '13 at 11:17