2

我正在我的 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;
}

编辑:

好的,似乎原因是其他设备上的 Twitter 帐户是在没有密码的情况下添加的。我去了设置应用程序,账户就在那里,没有密码。

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

4

1 回答 1

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]);
        });
    }
}];
//[...]
于 2013-07-10T14:27:53.200 回答