【问题标题】:twitter update using TWRequest gives 403 error使用 TWRequest 更新推特会出现 403 错误
【发布时间】:2012-11-30 09:39:09
【问题描述】:

我正在使用以下代码使用 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 分享并与其他用户多次提及。
  • 共享内容并不总是相同,因此不会出现重复问题,因为我们经常清理时间线。

谢谢

【问题讨论】:

  • 哪种方法导致错误?访问帐户或发布推文?错误还说了什么?
  • 您是否尝试过使用尚未发布在您帐户中的其他文本?
  • 是的,我尝试了不同的文本

标签: iphone objective-c ios twitter ios6


【解决方案1】:

您尝试发布的文本必须与您之前发布的内容相似,因此您会收到此错误。根据文档here,不能多次发布相同的文本。

编辑:

这也可能取决于他们网站上提到的推文总数。

【讨论】:

    【解决方案2】:

    我已经使用这种方法一个多月来测试应用程序,它曾经可以正常工作,没有任何问题。

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

    如果您使用其他帐户或其他电话并且代码有效,那么这是我朋友的唯一解释。

    祝你好运

    【讨论】:

      【解决方案3】:

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

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

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

      你应该尝试使用

      https://api.twitter.com/1.1/statuses/update.json (有一个recent change to the Twitter API。您现在可以仅使用 HTTPS 调用它。)

      这将指定 API 的最新版本。

      【讨论】:

      • 我为将来可能出现的问题更改了链接。但是一些推特账户仍然存在问题!并非测试设备上的所有帐户。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-07-21
      • 2012-10-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-13
      • 1970-01-01
      相关资源
      最近更新 更多