Here is my code for "follow on twitter" (iOS6+):
-(IBAction)twitterFollow
{
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
[accountStore requestAccessToAccountsWithType:accountType options:nil completion:^(BOOL granted, NSError *error) {
if(granted) {
// Get the list of Twitter accounts.
NSArray *accountsArray = [accountStore accountsWithAccountType:accountType];
NSLog(@"twitter accounts: %@", accountsArray);
if ([accountsArray count] > 0) {
// Grab the initial Twitter account to tweet from.
ACAccount *twitterAccount = [accountsArray objectAtIndex:0];
NSMutableDictionary *tempDict = [[NSMutableDictionary alloc] init];
[tempDict setValue:@"myapp" forKey:@"screen_name"];
[tempDict setValue:@"true" forKey:@"follow"];
//requestForServiceType
SLRequest *postRequest = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodPOST URL:[NSURL URLWithString:@"https://api.twitter.com/1/friendships/create.json"] parameters:tempDict];
[postRequest setAccount:twitterAccount];
[postRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
NSString *output = [NSString stringWithFormat:@"HTTP response status: %i Error %d", [urlResponse statusCode],error.code];
NSLog(@"%@error %@", output,error.description);
NSString *twitterErrorMessage = [error localizedDescription];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:kDefaultErrorText message:twitterErrorMessage delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert performSelectorOnMainThread:@selector(show)
withObject:nil
waitUntilDone:NO];
}];
}else{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:kDefaultErrorText message:kTextMessageNoTwitter delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert performSelectorOnMainThread:@selector(show)
withObject:nil
waitUntilDone:NO];
}
}
}];
}
This is what I get:
HTTP response status: 410 Error 0error (null)
What am I doing wrong?
The screen name and account are valid:
twitter accounts: (
"type:com.apple.twitter\nidentifier: 2ABCDEC6-9D6B-1234-9AB7-847EB167B123\naccountDescription: @theuser\nusername: theuser\nobjectID: x-coredata://ABD5C87A-FB5D-464E-AF18-2C1123451123/Account/p13\nenabledDataclasses: {(\n)}\nenableAndSyncableDataclasses: {(\n)}\nproperties: {\n fullName = theuser;\n \"user_id\" = 147712345;\n}\nparentAccount: (null)\nowningBundleID:(null)"
)
Well, I got rid of the 410 error by using the 1.1 API:
SLRequest *postRequest = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodPOST URL:[NSURL URLWithString:@"https://api.twitter.com/1.1/friendships/create.json"] parameters:tempDict];
Now I get a 400 error:
HTTP response status: 400 Error 0error (null)
Which suggested invalid authentication, even though the code above looks exactly like the code I see on every tutorial/example I find. I guess I'll create another question...