Reputation: 22559
I was developing and testing an app on my iPhone 4, and it was great. I tested my twitter code, and it worked well. I would retrieve the user's account, and then follow a certain account.
Today, I tried to install and test the app on two other devices, but it just ain't workin!
The response:
{"errors":[{"message":"Bad Authentication data","code":215}]}
The code:
- (void)followApp {
if (!self.account) {
[self _signInWithHandler:^{
[self followApp];
}];
return;
}
NSURL *feedURL = [NSURL URLWithString:@"https://api.twitter.com/1.1/friendships/create.json"];
NSDictionary *parameters = @{
@"follow" : @"true",
@"screen_name" : [MCAppManager sharedManager].applicationTwitterHandle
};
TWRequest *twitterFeed = [[TWRequest alloc] initWithURL:feedURL
parameters:parameters
requestMethod:TWRequestMethodPOST];
twitterFeed.account = self.account;
// Perform the twitter request
[twitterFeed performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
if (!error) {
NSLog(@"response: %@", [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]);
} else {
dispatch_async(dispatch_get_main_queue(), ^{
self.isFollowing = NO;
MCAlertError([error localizedDescription]);
});
}
}];
self.isFollowing = YES;
}
EDIT:
OK, it seems that the reason is that the twitter accounts on the other devices were added without their password. I went to the settings app, and the accounts were just there without passwords.
This isn't really the ultimate answer, but at least it explains where the error is coming from.
Upvotes: 2
Views: 2340
Reputation: 381
It seems that there is not a documented method to perform a credential check first.
You have to check the result and notify the user if there was an authorization error.
//[...]
[twitterFeed performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
if (error == nil) {
NSError *jsonError = nil;
id data = [NSJSONSerialization JSONObjectWithData:responseData
options:NSJSONReadingMutableLeaves
error:&jsonError];
if ([urlResponse statusCode] == 200) {
if (data) {
NSLog(@"response: %@", data);
} else {
dispatch_async(dispatch_get_main_queue(), ^{
self.isFollowing = NO;
MCAlertError([jsonError localizedDescription]);
});
}
} else if ([urlResponse statusCode] == 400) {
//Bad authorization data
if ([[[[data objectForKey:@"errors"] objectAtIndex:0] objectForKey:@"code"] intValue] == 215) {
dispatch_async(dispatch_get_main_queue(), ^{
self.isFollowing = NO;
MCAlertError(@"To use this feature you need a twitter account properly authenticated in your iOS settings");
});
self.account = nil;
} else {
dispatch_async(dispatch_get_main_queue(), ^{
self.isFollowing = NO;
MCAlertError(@"Generic service error");
});
}
} else {
dispatch_async(dispatch_get_main_queue(), ^{
self.isFollowing = NO;
MCAlertError(@"Generic service error");
});
}
} else {
dispatch_async(dispatch_get_main_queue(), ^{
self.isFollowing = NO;
MCAlertError([error localizedDescription]);
});
}
}];
//[...]
Upvotes: 3