Access Twitter User Timeline using Fabric SDK iOS -
i trying struggle issue 2 days. using fabric sdk , rest kit, trying play different rest api web services twitter. can login using twtrloginbutton
having session object authtokensecret
,authtoken
, other values. when try user timeline, failure response in return as:
{"errors":[{"code":215,"message":"bad authentication data.
"}]}
full error log is:
e restkit.network:rkobjectrequestoperation.m:297 object request failed: underlying http request operation failed 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] restkit.network:rkhttprequestoperation.m:154 '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 '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}
code:
- (void)viewdidload { [super viewdidload]; // additional setup after loading view, typically nib. [self addloginbutton]; } -(void) addloginbutton { twtrloginbutton *loginbutton = [twtrloginbutton buttonwithlogincompletion:^(twtrsession *session, nserror *error) { // play twitter session if(session) { nslog(@"logged in success! session : %@", session); [global sharedinstance].session = session; [self requestusertimeline]; } else { nslog(@"session 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); // 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 article: %@", timeline); } failure:^(rkobjectrequestoperation *operation, nserror *error) { nslog(@"failed error: %@", [error localizeddescription]); }]; [operation start]; }
please me in debugging problem. thanks.
after experimenting fabric sdk, successful in integration. came along conclusions, , want share guys.
1) when first time login twitter successfully, session of twtrsession
has been created user. lasts after close app , reopen it.
2) if session has been created you, , try login getting session object without logging out, authentication error returned.
3) can check if session exists or not using:
if([[twitter sharedinstance] session]) { nslog(@"session present!!!"); nslog(@"signed in %@", [[[twitter sharedinstance] session] username]); } else { nslog(@"you need login!!"); }
4) recommend login using
[[twitter sharedinstance] loginwithcompletion:^(twtrsession *session, nserror *error)];
instead of:
[twtrloginbutton buttonwithlogincompletion:^(twtrsession *session, nserror *error)];
use twitter's login button only, when sure no session exists currently.
5) if twitter's authentication teasing up, uninstall app , try fresh install. this last solution!
6) logout session, use [[twitter sharedinstance] logout];
coding part:
i assuming have followed steps fabric mac app.
first login user, make timeline request.
-(void) loginusertotwitter { if([[twitter sharedinstance] session]) { nslog(@"session present!!!"); nslog(@"signed in %@", [[[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 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 prefer twitter sdk's method extract tweets using [twtrtweet tweetswithjsonarray:arrayrep]
instead of restkit
. things easy handle here.
display tweets in twitter's standard style:
- (void)viewdidload { [super viewdidload]; // additional setup after loading view, typically 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 height of each row. must implement - (cgfloat)tableview:(uitableview *)tableview heightforrowatindexpath:(nsindexpath *)indexpath { twtrtweet *tweet = twitterresponse[indexpath.row]; return [twtrtweettableviewcell heightfortweet:tweet width:cgrectgetwidth(self.view.bounds)]; }
note:
download fabric sdk here. have enter email address. email link download, have follow steps. fabric mac app have configure xcode project.
hope helps!
references:
Comments
Post a Comment