iOS:错误验证数据,来自Twitter的代码215

时间:2013-01-18 18:45:39

标签: ios oauth twitter

我正在iPhone 4上开发和测试应用程序,这很棒。我测试了我的推特代码,效果很好。我会检索用户的帐户,然后关注某个帐户。

今天,我尝试在另外两台设备上安装和测试应用程序,但它只是不起作用

回应:

{"errors":[{"message":"Bad Authentication data","code":215}]}

代码:

- (void)followApp {
    if (!self.account) {
        [self _signInWithHandler:^{
            [self followApp];
        }];
        return;
    }

    NSURL *feedURL = [NSURL URLWithString:@"https://api.twitter.com/1.1/friendships/create.json"];
    NSDictionary *parameters = @{
    @"follow" : @"true",
    @"screen_name" : [MCAppManager sharedManager].applicationTwitterHandle
    };

    TWRequest *twitterFeed = [[TWRequest alloc] initWithURL:feedURL
                                                 parameters:parameters
                                              requestMethod:TWRequestMethodPOST];

    twitterFeed.account = self.account;
    // Perform the twitter request
    [twitterFeed performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
        if (!error) {
            NSLog(@"response: %@", [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]);
        } else {
            dispatch_async(dispatch_get_main_queue(), ^{
                self.isFollowing = NO;
                MCAlertError([error localizedDescription]);
            });
        }
    }];

    self.isFollowing = YES;
}

修改

好的,似乎原因是其他设备上的推特账号没有他们的密码。我去了设置应用程序,帐户就在那里没有密码。

这不是最终的答案,但至少它解释了错误的来源。

1 个答案:

答案 0 :(得分:3)

似乎没有文档化的方法来首先执行凭据检查。

您必须检查结果并通知用户是否存在授权错误。

//[...]
[twitterFeed performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {

    if (error == nil) {

        NSError *jsonError = nil;

        id data = [NSJSONSerialization JSONObjectWithData:responseData
                                                  options:NSJSONReadingMutableLeaves
                                                    error:&jsonError];
        if ([urlResponse statusCode] == 200) {

            if (data) {

                NSLog(@"response: %@", data);
            } else {

                dispatch_async(dispatch_get_main_queue(), ^{
                    self.isFollowing = NO;
                    MCAlertError([jsonError localizedDescription]);
                });
            }
        } else if ([urlResponse statusCode] == 400) {

            //Bad authorization data
            if ([[[[data objectForKey:@"errors"] objectAtIndex:0] objectForKey:@"code"] intValue] == 215) {

                dispatch_async(dispatch_get_main_queue(), ^{
                    self.isFollowing = NO;
                    MCAlertError(@"To use this feature you need a twitter account properly authenticated in your iOS settings");
                });
                self.account = nil;
            } else {

                dispatch_async(dispatch_get_main_queue(), ^{
                    self.isFollowing = NO;
                    MCAlertError(@"Generic service error");
                });
            }
        } else {

            dispatch_async(dispatch_get_main_queue(), ^{
                self.isFollowing = NO;
                MCAlertError(@"Generic service error");
            });
        }
    } else {
        dispatch_async(dispatch_get_main_queue(), ^{
            self.isFollowing = NO;
            MCAlertError([error localizedDescription]);
        });
    }
}];
//[...]