开发者

How to authenticate on twitter from iphone using asihttp?

I used the asihttp library to connect to twitter.

The idea is to send a login request, get the response and extract the session ID/auth code from the response's cookie header. Then you can use that session ID/auth code for 开发者_如何学Goconsecutive calls.

I don't obtain the auth_code because the authentication fails. how can I fix this?

the code is below:

 - (void) login {
 NSString *username = @"user";
 NSString *password = @"pass";
 NSURL *url = [NSURL URLWithString:@"https://twitter.com/sessions?phx=1"];

 ASIFormDataRequest *request = [[[ASIFormDataRequest alloc] initWithURL:url] autorelease]; 

 [request addRequestHeader:@"User-Agent" value: @"ASIHTTPRequest"];

 [request setPostValue:username forKey:@"session[username_or_email]"];
 [request setPostValue:password forKey:@"session[password]"];

 [request setDelegate: self];
 [request setDidFailSelector: @selector(loginRequestFailed:)];
 [request setDidFinishSelector: @selector(loginRequestFinished:)];

 [request startAsynchronous];
}

- (void)loginRequestFailed:(ASIHTTPRequest *)request {
  NSError *error = [request error];
  NSLog(@"login request failed with error: %@", [error localizedDescription]);  
}

- (void)loginRequestFinished:(ASIHTTPRequest *)request {
   NSString *responseString = [[request responseHeaders] objectForKey:@"Set-Cookie"];   
   NSLog(@"%@",responseString);
}

I tried to connect from shell and it works.

curl -d 'session[user_or_emai]=user&session[password]=pass' https://twitter.com/sessions


Don't scrape twitter.com. It will end with you getting suspended. Instead use the approved API to integrate with Twitter. You can read about how authentication works with Twitter's API, how you can use xAuth to jumpstart authentication with a users password, and the open source code to help get you started.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜