我已经为此问题奋斗了两天。我正在使用Fabric SDK和Rest kit,尝试使用不同的Twitter Rest API web服务。我可以成功登录使用TWTRLogInButton
并拥有带有authTokenSecret
、authToken
和其他值的会话对象。但当我尝试获取用户时间轴时,总是返回失败响应,如下所示:
{"errors":[{"code":215,"message":"Bad Authentication data.
"}]}
完整的错误日志如下:
E restkit.network:RKObjectRequestOperation.m:297 Object request failed: Underlying HTTP request operation failed with error: Error Domain=org.restkit.RestKit.ErrorDomain Code=-1011 "Expected status code in (200-299), got 400" UserInfo=0x1780f6f80 {NSLocalizedRecoverySuggestion={"errors":[{"code":215,"message":"Bad Authentication data."}]}, NSErrorFailingURLKey=https://api.twitter.com/1.1/statuses/user_timeline.json?user_id=3116882322&count=2&screen_name=ann_10p, AFNetworkingOperationFailingURLRequestErrorKey=<NSMutableURLRequest: 0x178202740> { URL: https://api.twitter.com/1.1/statuses/user_timeline.json?user_id=3116882322&count=2&screen_name=ann_10p }, AFNetworkingOperationFailingURLResponseErrorKey=<NSHTTPURLResponse: 0x1702271e0> { URL: https://api.twitter.com/1.1/statuses/user_timeline.json?user_id=3116882322&count=2&screen_name=ann_10p } { status code: 400, headers {
"Content-Encoding" = gzip;
"Content-Length" = 87;
"Content-Type" = "application/json;charset=utf-8";
Date = "Wed, 01 Apr 2015 09:46:42 GMT";
Server = "tsa_a";
"Strict-Transport-Security" = "max-age=631138519";
"x-connection-hash" = 4c123a59a023cd86b2e9a3e9fc84cd7b;
"x-response-time" = 4;
} }, NSLocalizedDescription=Expected status code in (200-299), got 400}
2015-04-01 14:47:13.223 TwitterIntegration[1086:60b] I restkit.network:RKHTTPRequestOperation.m:154 GET 'https://api.twitter.com/1.1/statuses/user_timeline.json?user_id=3116882322&count=2&screen_name=ann_10p'
2015-04-01 14:47:13.225 TwitterIntegration[1086:60b] E restkit.network:RKHTTPRequestOperation.m:178 GET 'https://api.twitter.com/1.1/statuses/user_timeline.json?user_id=3116882322&count=2&screen_name=ann_10p' (400 Bad Request) [0.0013 s]: Error Domain=org.restkit.RestKit.ErrorDomain Code=-1011 "Expected status code in (200-299), got 400" UserInfo=0x1780f6f80 {NSLocalizedRecoverySuggestion={"errors":[{"code":215,"message":"Bad Authentication data."}]}, NSErrorFailingURLKey=https://api.twitter.com/1.1/statuses/user_timeline.json?user_id=3116882322&count=2&screen_name=ann_10p, AFNetworkingOperationFailingURLRequestErrorKey=<NSMutableURLRequest: 0x178202740> { URL: https://api.twitter.com/1.1/statuses/user_timeline.json?user_id=3116882322&count=2&screen_name=ann_10p }, AFNetworkingOperationFailingURLResponseErrorKey=<NSHTTPURLResponse: 0x1702271e0> { URL: https://api.twitter.com/1.1/statuses/user_timeline.json?user_id=3116882322&count=2&screen_name=ann_10p } { status code: 400, headers {
"Content-Encoding" = gzip;
"Content-Length" = 87;
"Content-Type" = "application/json;charset=utf-8";
Date = "Wed, 01 Apr 2015 09:46:42 GMT";
Server = "tsa_a";
"Strict-Transport-Security" = "max-age=631138519";
"x-connection-hash" = 4c123a59a023cd86b2e9a3e9fc84cd7b;
"x-response-time" = 4;
} }, NSLocalizedDescription=Expected status code in (200-299), got 400}
代码:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self addLoginButton];
}
-(void) addLoginButton
{
TWTRLogInButton *logInButton = [TWTRLogInButton buttonWithLogInCompletion:^(TWTRSession *session, NSError *error) {
// play with Twitter session
if(session)
{
NSLog(@"logged in success! with session : %@", session);
[Global sharedInstance].session = session;
[self requestUserTimeline];
}
else
{
NSLog(@"session is null");
}
}];
logInButton.center = self.view.center;
[self.view addSubview:logInButton];
}
-(void) requestUserTimeline
{
RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[UserTimeline class]];
[mapping addAttributeMappingsFromDictionary:@{
@"text": @"tweetText",
@"favorited": @"favourited",
@"created_at": @"createdAt",
@"user.name": @"name",
@"id": @"tweetID",
@"user.profile_image_url": @"profileImageURL"
}];
NSIndexSet *statusCodes = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful); // Anything in 2xx
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:mapping pathPattern:nil keyPath:nil statusCodes:statusCodes];
NSString *params = [NSString stringWithFormat:@"?user_id=3116882322&count=2&screen_name=ann_10p",[Global sharedInstance].session.userID,[Global sharedInstance].session.userName];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[@"https://api.twitter.com/1.1/statuses/user_timeline.json" stringByAppendingString:params]]];
[request setHTTPMethod:@"GET"];
RKObjectRequestOperation *operation = [[RKObjectRequestOperation alloc] initWithRequest:request responseDescriptors:@[responseDescriptor]];
[operation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *result) {
UserTimeline *timeline = [result firstObject];
NSLog(@"Mapped the article: %@", timeline);
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
NSLog(@"Failed with error: %@", [error localizedDescription]);
}];
[operation start];
}
请帮忙调试这个问题。谢谢。