Twitter更新使用TWRequest时出现403错误

4
我使用以下代码来在iOS 5的Twitter API中发布推文到用户的时间线上:
// 创建一个账户存储对象。 ACAccountStore *accountStore = [[ACAccountStore alloc] init];
// Create an account type that ensures Twitter accounts are retrieved.
ACAccountType *accountType =  [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];

// Request access from the user to use their Twitter accounts.
[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.
         // Grab the initial Twitter account to tweet from.
         ACAccount *twitterAccount = [accountsArray objectAtIndex:buttonIndex];
         // Create a request, which in this example, posts a tweet to the user's timeline.
         // This example uses version 1 of the Twitter API.
         // This may need to be changed to whichever version is currently appropriate.


         TWRequest *postRequest = [[TWRequest alloc] initWithURL:[NSURL URLWithString:@"http://api.twitter.com/1/statuses/update.json"] parameters:[NSDictionary dictionaryWithObject:@"hello this is a tweet" forKey:@"status"] requestMethod:TWRequestMethodPOST];

         // Set the account used to post the tweet.
         [postRequest setAccount:twitterAccount];

         // Perform the request created above and create a handler block to handle the response.
         [postRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) 
          {

              NSString *output = [NSString stringWithFormat:@"%i", [urlResponse statusCode]];
              [self performSelectorOnMainThread:@selector(TweetStatus:) withObject:output waitUntilDone:NO];
          }];
     }
 }];

我已经使用这种方法超过一个月来测试应用程序,之前一切正常没有问题。

几周前,以下方法开始返回403错误。

[postRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) 
          {

              NSString *output = [NSString stringWithFormat:@"%i", [urlResponse statusCode]];
              [self performSelectorOnMainThread:@selector(TweetStatus:) withObject:output waitUntilDone:NO];
          }];

它给出以下错误:

Error Domain=NSURLErrorDomain Code=-1012 "The operation couldn’t be completed. (NSURLErrorDomain error -1012.)" UserInfo=0x1f8b3250 {NSErrorFailingURLKey=http://api.twitter.com/1/statuses/update.json, NSErrorFailingURLStringKey=http://api.twitter.com/1/statuses/update.json, NSUnderlyingError=0x1ed19f90 "The operation couldn’t be completed. (kCFErrorDomainCFNetwork error -1012

我已经搜索了很多,但并没有找到任何解决方案或真正的原因。

以下是需要注意的几点,这可能有助于理解问题:

  • 该应用程序用于测试,这意味着我们与其他用户经常使用 Twitter 分享和提及功能。
  • 分享内容并不总是相同的,因此不应出现重复问题,因为我们经常清除时间线。

谢谢


是哪个方法导致了错误?访问账户还是发布推文?还有错误信息显示什么? - hwaxxer
你尝试使用一些还未在你账号上发布过的文字了吗? - P.J
是的,我尝试了不同的文本。 - Kassem
3个回答

7
您一直试图发推文的文本必须与您先前发布的推文类似,因此出现了此错误。根据此处的文档,相同的文本不能重复发布。总发布推文的数量也可能会影响此问题。 enter image description here

6

我已经使用这种方法超过一个月来测试应用程序,以前一直没有问题。

我认为问题就在这里。你正在测试的这个帐户可能已被其他人标记为垃圾邮件发送者。

如果你使用另一个帐户或其他手机,而代码可以工作,那么这就是唯一的解释,我的朋友。

祝你好运。


4

我注意到您正在使用以下URL来发布来自您的用户的推文:

http://api.twitter.com/1/statuses/update.json

这里使用的是 Twitter API 的 1.0 版本,该版本已经被弃用,并将在接下来的几个月内逐渐停止工作。

相反,您应该尝试使用

https://api.twitter.com/1.1/statuses/update.json (Twitter API 最近有更新。现在您只能通过 HTTPS 调用它。)

这将指定最新版本的 API。


我已经更改了链接,以解决未来可能出现的问题。但是,一些 Twitter 帐户仍存在问题!并非所有测试设备上的帐户都有这个问题。 - Kassem

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接