Trying to "follow" someone on Twitter using new iOS 5 API, getting 406 return error. Why?
Is my code correct? Need to find out why this isn't working....
- (void)followOnTwitter:(id)sender
{
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
[accountStore requestAccessToAccountsWithType:accountType withCompletionHandler:^(BOOL granted, NSError *error) {
if(granted) {
// Get the list of Twitter accounts.
NSArray *accountsArray = [accountStore accountsWithAccountType:accountType];
// For the sake of brevity, we'll assume there is only one Twitter account present.
// You would ideally ask the user which account they want to tweet from, if there is more than one Twitter account present.
if ([accountsArray count] > 0) {
// Grab the initial Twitter account to tweet from.
ACAccount *twitterAccount = [accountsArray objectAtIndex:0];
NSMutableDictionary *tempDict = [[NSMutableDictionary alloc] init];
[tempDict setValue:@"sortitapps" forKey:@"screen_name"];
[tempDict setValue:@"true" forKey:@"follow"];
TWRequest *postRequest = [[TWRequest alloc] initWithURL:[NSURL URLWithString:@"http://api.twitter.com/1/friendships/create.format"]
parameters:tempDict
requestMethod:TWRequestMethodPOST];
[postRequest setAccount:twitterAccount];
[postRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
NSString *output = [NSString stringWithFormat:@"HTTP response status: %i", [urlResponse statusCode]];
NSLog(@"%@", output);
}];
}
}
}];
}
All of the code looks correct. Are the parameters incorrect? Is the URL correct? Need some direction here....
Found the answer to my own question... I changed the URL to https://api.twitter.com/1/friendships/create.json and it worked.
Don't forget it's https, not just http.
For iOS 6 twitter follow
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];
// For the sake of brevity, we'll assume there is only one Twitter account present.
// You would ideally ask the user which account they want to tweet from, if there is more than one Twitter account present.
if ([accountsArray count] > 0) {
// Grab the initial Twitter account to tweet from.
ACAccount *twitterAccount = [accountsArray objectAtIndex:0];
NSMutableDictionary *tempDict = [[NSMutableDictionary alloc] init];
[tempDict setValue:@"MohammadMasudRa" forKey:@"screen_name"];
[tempDict setValue:@"true" forKey:@"follow"];
NSLog(@"*******tempDict %@*******",tempDict);
//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);
}];
}
}
}];
As twitter has updated their API to v1.1, the request should be made now using the URL below. Note that request should be with 'https://' and due to API v1.1 replace your 1 to 1.1 in your URL.
https://api.twitter.com/1.1/friendships/create.json
you can use this code
- (BOOL)openTwitterClientForUserName:(NSString*)userName {
NSArray *urls = [NSArray arrayWithObjects:
@"twitter://user?screen_name={username}", // Twitter
@"tweetbot:///user_profile/{username}", // TweetBot
@"echofon:///user_timeline?{username}", // Echofon
@"twit:///user?screen_name={username}", // Twittelator Pro
@"x-seesmic://twitter_profile?twitter_screen_name={username}", // Seesmic
@"x-birdfeed://user?screen_name={username}", // Birdfeed
@"tweetings:///user?screen_name={username}", // Tweetings
@"simplytweet:?link=http://twitter.com/{username}", // SimplyTweet
@"icebird://user?screen_name={username}", // IceBird
@"fluttr://user/{username}", // Fluttr
/** uncomment if you don't have a special handling for no registered twitter clients */
//@"http://twitter.com/{username}", // Web fallback,
nil];
UIApplication *application = [UIApplication sharedApplication];
for (NSString *urlString in urls) {
NSString *candidate = [urlString stringByReplacingOccurrencesOfString:@"{username}" withString:userName];
NSURL *url = [NSURL URLWithString:candidate];
if ([application canOpenURL:url]) {
[application openURL:url];
return YES;
}
}
return NO;
}
https://gist.github.com/ChrisRicca/9144169
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With