2011-11-10 6 views
28

新しいiOS 5 APIを使用してTwitter上の誰かに「フォロー」しようとすると、406エラーが返されます。どうして?新しいiOS 5 APIを使用してTwitterの誰かに「フォロー」しようとすると、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は正しいですか?ここにいくつかの方向が必要です....

+0

としては、以下の説明、エラーが '' 'HTTPである必要があり、間違ったURLから来ている://api.twitter。 http:// api.twitter.com/1/friendships/create.format''' – Martin

+0

以下で説明するように、httpsではなく、上記のコメントですので、com/1/friendships/create.json'''を '' '間違っています –

答えて

20

私自身の質問への答えを見つけました... URLをhttps://api.twitter.com/1/friendships/create.jsonに変更しました。

httpだけでなく、httpsも忘れないようにしてください。さえずりがV1.1に彼らのAPIを更新したようにiOSの6 Twitterに

+0

https:// infrontを追加するまで、これは私のためには機能しませんでした。 URL。 –

+0

ちょうどそこに気付いた、元気だったので、あなたはURLに置き換えてその部分を省略しないでください。明確にするために、回答を編集することもできます。素晴らしい、ありがとう。 –

20

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

いいね、Amit、ありがとう! –

+0

答えAmitに感謝します。クイック解明 - これはまずツイッターで認証が必要ですか? – Prasanth

2

に従って、要求は現在、以下のURLを使用して行われるべきです。リクエストは 'https://'でなければならないことに注意してください.API v1.1により、URLの1から1.1を置き換えてください。

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

+0

上記のURLは、iOS6以上での使用をお勧めします。 –

1

あなたはこのコードを使用することができます

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

このリンクは質問に答えるかもしれませんが、答えの本質的な部分をここに含めて参考にしてください。リンクされたページが変更された場合、リンクのみの回答は無効になります。 –

+0

ありがとうございました – SyraKozZ

+0

このコードを動作させるには、 'info.plist'ファイルを編集し、URL接頭辞を' LSApplicationQueriesSchemes'という配列のエントリとして追加する必要があります。 –

関連する問題