Get Twitter oauth key in similar way to Facebook oauth key
I have an iPhone app which current开发者_开发问答ly loads up a UIWebView using the facebook graph api oauth url (https://graph.facebook.com/oauth/authorize?...) - a user then enters their login details, and on submitting I retrieve the the access token from the URL. (In a manner as this I believe http://www.capturetheconversation.com/technology/iphone-facebook-oauth2-graph-api) This is then passed to my java server which performs the various actions using RestFB
My question is, is there a similar way to do this with Twitter? Currently it seems the only way to get the data is to download a library and use that; yet this is an approach I would rather not use as it would take up unnecessary space seeing as all the commands will actually be run from my Java server.
Any advice would be greatly appreciated
Thanks,
Dan
Twitter uses OAuth 1.0a authentication, but not OAuth 2.0.
Steps to get an access token on Twitter:
- Create your own application on Twitter and get consumer key and secret.
- Download open source OAuth library, OAuthConsumer in my case.
- Write registration code, see example below.
p.s. Set application type as web application, it turns on callback instead of PIN code.
static NSString* kMyApplicationConsumerKey = @"XXXXXXXXXXX";
static NSString* kMyApplicationConsumerSecret = @"XXXXXXXXXXX";
@interface TwitterConnector : NSObject {
OAConsumer* consumer;
OAToken* requestToken;
OAToken* accessToken;
}
- (void)start;
@end
@implementation TwitterConnector
- (void)start {
consumer = [[OAConsumer alloc] initWithKey:kMyApplicationConsumerKey secret:kMyApplicationConsumerSecret];
NSURL* requestTokenUrl = [NSURL URLWithString:@"http://api.twitter.com/oauth/request_token"];
OAMutableURLRequest* requestTokenRequest = [[[OAMutableURLRequest alloc] initWithURL:requestTokenUrl
consumer:consumer
token:nil
realm:nil
signatureProvider:nil] autorelease];
OARequestParameter* callbackParam = [[[OARequestParameter alloc] initWithName:@"oauth_callback" value:@"twitter://authorized"] autorelease];
[requestTokenRequest setHTTPMethod:@"POST"];
[requestTokenRequest setParameters:[NSArray arrayWithObject:callbackParam]];
OADataFetcher* dataFetcher = [[[OADataFetcher alloc] init] autorelease];
[dataFetcher fetchDataWithRequest:requestTokenRequest
delegate:self
didFinishSelector:@selector(didReceiveRequestToken:data:)
didFailSelector:@selector(didFailOAuth:error:)];
}
- (void)didReceiveRequestToken:(OAServiceTicket*)ticket data:(NSData*)data {
NSString* httpBody = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];
requestToken = [[OAToken alloc] initWithHTTPResponseBody:httpBody];
NSURL* authorizeUrl = [NSURL URLWithString:@"https://api.twitter.com/oauth/authorize"];
OAMutableURLRequest* authorizeRequest = [[[OAMutableURLRequest alloc] initWithURL:authorizeUrl
consumer:nil
token:nil
realm:nil
signatureProvider:nil] autorelease];
NSString* oauthToken = requestToken.key;
OARequestParameter* oauthTokenParam = [[[OARequestParameter alloc] initWithName:@"oauth_token" value:oauthToken] autorelease];
[authorizeRequest setParameters:[NSArray arrayWithObject:oauthTokenParam]];
UIWebView* webView = [[UIWebView alloc] initWithFrame:[UIScreen mainScreen].bounds];
[[[UIApplication sharedApplication] keyWindow] addSubview:webView];
[webView release];
webView.delegate = self;
[webView loadRequest:authorizeRequest];
}
- (void)didReceiveAccessToken:(OAServiceTicket*)ticket data:(NSData*)data {
NSString* httpBody = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];
accessToken = [[OAToken alloc] initWithHTTPResponseBody:httpBody];
// FINISHED!
}
- (void)didFailOAuth:(OAServiceTicket*)ticket error:(NSError*)error {
// ERROR!
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark UIWebViewDelegate
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
if ([[[request URL] scheme] isEqualToString:@"twitter"]) {
// Extract oauth_verifier from URL query
NSString* verifier = nil;
NSArray* urlParams = [[[request URL] query] componentsSeparatedByString:@"&"];
for (NSString* param in urlParams) {
NSArray* keyValue = [param componentsSeparatedByString:@"="];
NSString* key = [keyValue objectAtIndex:0];
if ([key isEqualToString:@"oauth_verifier"]) {
verifier = [keyValue objectAtIndex:1];
break;
}
}
if (verifier) {
NSURL* accessTokenUrl = [NSURL URLWithString:@"https://api.twitter.com/oauth/access_token"];
OAMutableURLRequest* accessTokenRequest = [[[OAMutableURLRequest alloc] initWithURL:accessTokenUrl
consumer:consumer
token:requestToken
realm:nil
signatureProvider:nil] autorelease];
OARequestParameter* verifierParam = [[[OARequestParameter alloc] initWithName:@"oauth_verifier" value:verifier] autorelease];
[accessTokenRequest setHTTPMethod:@"POST"];
[accessTokenRequest setParameters:[NSArray arrayWithObject:verifierParam]];
OADataFetcher* dataFetcher = [[[OADataFetcher alloc] init] autorelease];
[dataFetcher fetchDataWithRequest:accessTokenRequest
delegate:self
didFinishSelector:@selector(didReceiveAccessToken:data:)
didFailSelector:@selector(didFailOAuth:error:)];
} else {
// ERROR!
}
[webView removeFromSuperview];
return NO;
}
return YES;
}
- (void)webView:(UIWebView*)webView didFailLoadWithError:(NSError*)error {
// ERROR!
}
@end
精彩评论