【问题标题】:IOS can't reply status with status_idIOS 无法使用 status_id 回复状态
【发布时间】:2014-06-02 23:09:45
【问题描述】:

我想用状态 ID 回复 Twitter 状态。但我得到 http 响应 403。 这是我的代码。我可以发送第一条推文。我知道它的id。但是当我想用它的 id 回复第一条推文时,我得到 http 响应 403。我错了。

 - (IBAction)btnSendToucUpInside:(id)sender {

    __block NSDecimalNumber *tid;
    __block int i = 0;
    while (true) {
        if (i == 0  || _birlestirilsinMi) {
            urlParametres = [NSDictionary dictionaryWithObjectsAndKeys: [NSString stringWithFormat:@"%@", [self.twitArray objectAtIndex:0]], @"status", nil];
        }else{
            urlParametres = [NSDictionary dictionaryWithObjectsAndKeys: [NSString stringWithFormat:@"%@", [self.twitArray objectAtIndex:0]], @"status",[NSString stringWithFormat:@"%@",tid], @"in_reply_to_status_id",@"true", @"include_entities", nil];
        }
        TWRequest *postRequest = [[TWRequest alloc] initWithURL:[NSURL URLWithString:@"https://api.twitter.com/1/statuses/update.json"] parameters:urlParametres requestMethod:TWRequestMethodPOST];

        [postRequest setAccount:[twitAccountArray objectAtIndex:0]];
        [postRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error)
         {
             if (responseData) {
                 i++;
                 NSDictionary *TWData = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableLeaves error:&error];

                 tid = TWData[@"id"];
                 NSLog(@"%@",tid);

                 //show status after done
                 NSString *output = [NSString stringWithFormat:@"HTTP response status: %i", [urlResponse statusCode]];
                 NSLog(@"Twiter post status : %@", output);
             }
         }];

        if (i == _twitArray.count -1) {
            break;
        }
    }
}

【问题讨论】:

  • 403 表示未经授权,请仔细检查[twitAccountArray objectAtIndex:0] 中的内容可能是什么?
  • @VincentG 我检查了。我发送没有循环的推文我可以发送每个帖子。
  • 我调试了,我得到错误:响应数据中的重复状态
  • 那么可能只是 Twitter 拒绝您发布两次相同的消息? :)
  • 是的。我认为问题是while循环。因为while循环不等待响应。但我不知道另一种方式。你有关于这个@VincentG的任何解决方案

标签: ios twitter


【解决方案1】:

您反复向 twitter 发送相同的消息,您的循环不稳定并且没有正确使用您的 twitArray 变量。确保在构建 TWRequest 实例时使用 twitArray。你需要这样的东西:

for (NSString *twit in twitArray) {
    ...
    urlParametres = @{ @"status" : twit }
    ...
}

而不是在每次迭代中使用[self.twitArray objectAtIndex:0]...


还要照顾TWRequestTWRequest is deprecated in iOS 6.0 - what can I use instead?


有点离题,但while(true) + break 是一个非常糟糕的做法,几乎和使用goto 一样糟糕,你绝对应该使用类似的东西:

for (NSString *twit in twitArray) {...}

或者如果您需要相反的顺序:

for (NSString *twit in twitArray.reverseObjectEnumerator) {...}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-09
    • 2015-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多