Access a user’s custom timeline using the iOS Fabric SDK

I have been trying to deal with this issue for two days. I am using the Fabric SDK and Rest, trying to play with various Rest API web services for Twitter. I can successfully log in using TWTRLogInButton having a session object with authTokenSecret , authToken and other values. When I try to get the user's timeline, I always get a failure response in quality:

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

Full error log:

 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} 

The code:

 - (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]; } 

Please help me debug this problem. Thanks.

+7
ios twitter restkit fabric-twitter
source share
3 answers

After experimenting with the Fabric SDK, I was successful in integrating it. I came with some conclusions and I want to share with you guys.

1) When you first logged in to Twitter successfully, a TWTRSession was created for the user. It lasts even after the application is closed and reopened.

2) If a session has already been created for you, and you are trying to log in to get another session object without logging out, an authentication error will be returned.

3) You can check if the session exists or not:

 if([[Twitter sharedInstance] session]) { NSLog(@"session already present!!!"); NSLog(@"signed in as %@", [[[Twitter sharedInstance] session] userName]); } else { NSLog(@"you need to login!!"); } 

4) I recommend logging in with

[[Twitter sharedInstance] logInWithCompletion:^(TWTRSession *session, NSError *error)];

instead:

[TWTRLogInButton buttonWithLogInCompletion:^(TWTRSession *session, NSError *error)];

Use the Twitter login button only if you are sure that a session is absent currently.

5) If Twitter authentication really teases you, uninstall the application and try with a fresh installation. This is the last decision!

6) To log out, use [[Twitter sharedInstance] logOut];

The coding part:

I assume that you have already completed all the steps of the mac mac application.

Log in first, then query the timeline.

 -(void) loginUserToTwitter { if([[Twitter sharedInstance] session]) { NSLog(@"session already present!!!"); NSLog(@"signed in as %@", [[[Twitter sharedInstance] session] userName]); [self getUserTimeline]; } else { NSLog(@"session not found. Make new request!"); [[Twitter sharedInstance] logInWithCompletion:^(TWTRSession *session, NSError *error) { if(error) NSLog(@"error occurred... %@",error.localizedDescription); else { NSLog(@"Successfully logged in with session :%@",session); [self getUserTimeline]; } }]; } } -(void) getUserTimeline { NSURLRequest *request = [[[Twitter sharedInstance] APIClient] URLRequestWithMethod:@"GET" URL:@"https://api.twitter.com/1.1/statuses/user_timeline.json" parameters:@{@"userid": [Twitter sharedInstance].session.userID, @"count" : @"5", @"screen_name" : [Twitter sharedInstance].session.userName} error:nil]; NSURLResponse *response; NSError *error; NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; if(!data) { NSLog(@"error....: %@",error.localizedDescription); } else { NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; NSLog(@"%@",string); [twitterResponse removeAllObjects]; NSArray *arrayRep = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil]; twitterResponse = [NSMutableArray arrayWithArray:[TWTRTweet tweetsWithJSONArray:arrayRep]]; [_tableView reloadData]; } } 

I would rather use the Twitter SDK method to extract tweets using [TWTRTweet tweetsWithJSONArray:arrayRep] instead of Restkit . It will be very easy to handle here.

Show Twitter Tweets Standard Style:

 - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. // Setup tableview self.tableView.estimatedRowHeight = 150; self.tableView.rowHeight = UITableViewAutomaticDimension; // Explicitly set on iOS 8 if using automatic row height calculation self.tableView.allowsSelection = NO; [self.tableView registerClass:[TWTRTweetTableViewCell class] forCellReuseIdentifier:@"TweetCell"]; } #pragma mark - Tableview Methods - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return twitterResponse.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *cellID = @"TweetCell"; TWTRTweetTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath]; TWTRTweet *tweet = twitterResponse[indexPath.row]; [cell configureWithTweet:tweet]; return cell; } // Calculate the height of each row. Must to implement - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { TWTRTweet *tweet = twitterResponse[indexPath.row]; return [TWTRTweetTableViewCell heightForTweet:tweet width:CGRectGetWidth(self.view.bounds)]; } 

Note:

Download the Fabric SDK from here . You will need to enter an email address. They will email you a download link, you need to follow a few steps. The Fabric Mac app offers you to fully configure your xcode project.

Hope this helps!

Literature:

Twitter login

Show tweets

Cannonball Project Example

+9
source share

https://github.com/fhsjaagshs/FHSTwitterEngine

try this FHSTTwitterEngine to work fine

+1
source share

You can directly show the user’s timeline . No need to directly access login or other network calls. This automatically processes Guest Auth and then loads the initial tweets and also loads more when the end of the UITableView reached:

 class UserTimelineViewController: TWTRTimelineViewController, TWTRTweetViewDelegate { convenience init() { // Set up the User Timeline let dataSource = TWTRUserTimelineDataSource(screenName: "TomCruise", APIClient: TWTRAPIClient()) // Set the data source (will automatically load Tweets in `viewWillAppear` self.init(dataSource: dataSource) // Update the title displayed in the Navigation bar self.title = "@\(dataSource.screenName)" } func tweetView(tweetView: TWTRTweetView, didSelectTweet tweet: TWTRTweet) { // Log a message when a cell is tapped print("Selected tweet with ID: \(tweet.tweetID)") } } 
0
source share

All Articles