我有一个iPhone应用程序,当前使用facebook图形api oauth url(https://graph.facebook.com/oauth/authorize?...)加载UIWebView - 用户然后输入他们的登录详细信息,并提交我从URL中检索访问令牌.(以这种方式我相信http://www.capturetheconversation.com/technology/iphone-facebook-oauth2-graph-api)然后将其传递给我的java服务器,它使用RestFB执行各种操作
我的问题是,有没有类似的方式与Twitter这样做?目前似乎获取数据的唯一方法是下载库并使用它; 然而,这是一种我不愿意使用的方法,因为它会占用不必要的空间,因为所有命令实际上都是从我的Java服务器运行的.
任何建议将不胜感激
谢谢,
担
Twitter使用OAuth 1.0a 身份验证,但不使用OAuth 2.0.
在Twitter上获取访问令牌的步骤:
在Twitter上创建自己的应用程序,获取消费者密钥和秘密.
在我的案例中下载开源OAuth库,OAuthConsumer.
写注册码,见下面的例子.
ps将应用程序类型设置为Web应用程序,它将打开回调而不是PIN代码.
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