Попытка «подписаться» на кого-то в Твиттере с использованием нового API iOS 5, возвращает ошибку 406.Зачем? - PullRequest
28 голосов
/ 11 ноября 2011

Попытка «подписаться» на кого-то в Твиттере с использованием нового API iOS 5, ошибка возврата 406.Почему?

Мой код правильный?Нужно выяснить, почему это не работает ....

    - (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);

                }];
            }
        }
    }];
}

Весь код выглядит правильно.Параметры неверны?URL правильный?Здесь нужно направление ...

Ответы [ 4 ]

20 голосов
/ 01 ноября 2012

Для iOS 6 twitter следуйте

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);
            }];
        }

    }
}];
20 голосов
/ 16 ноября 2011

Нашел ответ на свой вопрос ... Я изменил URL на https://api.twitter.com/1/friendships/create.json, и это сработало.

Не забывайте, что это https, а не только http.

2 голосов
/ 01 февраля 2014

Поскольку твиттер обновил свой API до версии 1.1, запрос должен быть сделан сейчас, используя приведенный ниже URL. Обратите внимание, что запрос должен быть с 'https://' и из-за API v1.1 замените ваши 1 на 1.1 в вашем URL.

https://api.twitter.com/1.1/friendships/create.json

1 голос
/ 02 февраля 2015

Вы можете использовать этот код

- (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

...