2011-11-10 9 views
28

Cercando di "seguire" qualcuno su Twitter utilizzando la nuova API iOS 5, ottenendo 406 errore di ritorno. Perché?Cercando di "seguire" qualcuno su Twitter utilizzando la nuova API iOS 5, ottenendo 406 errore di ritorno. Perché?

Il mio codice è corretto? Hai bisogno di scoprire perché questo non funziona ....

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

       }]; 
      } 
     } 
    }]; 
} 

Tutto il codice sembra corretto. I parametri sono errati? L'URL è corretto? Ho bisogno di qualche direzione qui ....

+0

Come spiegare di seguito, l'errore viene dal URL sbagliata che dovrebbe essere '' 'http: //api.twitter. it/1/friendships/create.json''' invece di '' 'http: // api.twitter.com/1/friendships/create.format''' – Martin

+0

Come spiegato di seguito, è https non http quindi il commento sopra è sbagliato –

risposta

20

Trovato la risposta alla mia stessa domanda ... Ho cambiato l'URL a https://api.twitter.com/1/friendships/create.json e ha funzionato.

Non dimenticare che è https, non solo http.

+0

Questo non funzionava per me fino a quando ho aggiunto https: // infront di l'URL. –

+0

Appena notato che era lì in origine, nota rapida in modo da non essere sostituito con l'URL e omettere quella parte. Potresti voler modificare la risposta per chiarezza. Funziona alla grande, grazie. –

20

Per iOS 6 Twitter seguire

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

    } 
}]; 
+0

Bello, Amit, grazie! –

+0

grazie per la risposta Amit. Chiarificazione rapida: è necessario prima l'autenticazione con Twitter? – Prasanth

2

come Twitter ha aggiornato il suo API per v1.1, la richiesta dovrebbe essere fatta ora utilizzando l'URL di seguito. Tieni presente che la richiesta deve essere impostata su "https: //" e, a causa dell'API v1.1, sostituire il tuo 1 a 1.1 nel tuo URL.

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

+0

L'URL sopra riportato è raccomandato per iOS6 e versioni successive. –

1

è possibile utilizzare questo codice

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

+0

Sebbene questo collegamento possa rispondere alla domanda, è meglio includere qui le parti essenziali della risposta e fornire il link per riferimento. Le risposte di solo collegamento possono diventare non valide se la pagina collegata cambia. –

+0

grazie risolto – SyraKozZ

+0

Per far funzionare questo codice è necessario modificare il file 'info.plist' e aggiungere i prefissi url come voci in un array chiamato:' LSApplicationQueriesSchemes'. –

Problemi correlati