在 UIWebView 中使用 Tumblr 登录
Login With Tumblr in UIWebView
我正在通过 oauth 使用 tumbler 登录身份验证。我正在关注这个 url:http://codegerms.com/login-with-tumblr-in-uiwebview-using-xcode-6-part-3/
用于登录身份验证,并通过在 UIWebview 中登录,在 iOS 中获取 Tumblr API 的访问令牌和密钥。
我正在使用这段代码。
- (void)viewDidLoad {
[super viewDidLoad];
// clientID = @"Tjta51N6kF6Oxmm1f3ytpUvMPRAE1bRgCgG90SOa0bJMlSlLeT";
// secret = @"lrlQPNx3Yb1nRxp4qreYXvUURkGUmYCBoQacOmLTDRJAc7awRN";
clientID = @"sdF0Y6bQoJYwfIB1Mp7WECwobAgnq5tmkRjo7OXyKHDg3opY7Y";
secret = @"qJNGrRjyriZBeBhcgJz0MAcD9WAYXUW1tLbLrbYE4ZclzAUH9g";
redirect = @"tumblr://authorized";
[self.WebView setBackgroundColor:[UIColor clearColor]];
[self.WebView setOpaque:NO];
[self connectTumblr];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)connectTumblr {
consumer = [[OAConsumer alloc]initWithKey:clientID secret:secret];
NSURL* requestTokenUrl = [NSURL URLWithString:@"http://www.tumblr.com/oauth/request_token"];
OAMutableURLRequest* requestTokenRequest = [[OAMutableURLRequest alloc] initWithURL:requestTokenUrl
consumer:consumer
token:nil
realm:nil
signatureProvider:nil] ;
OARequestParameter* callbackParam = [[OARequestParameter alloc] initWithName:@"oauth_callback" value:redirect] ;
[requestTokenRequest setHTTPMethod:@"POST"];
[requestTokenRequest setParameters:[NSArray arrayWithObject:callbackParam]];
OADataFetcher* dataFetcher = [[OADataFetcher alloc] init] ;
[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];
requestToken = [[OAToken alloc] initWithHTTPResponseBody:httpBody];
NSURL* authorizeUrl = [NSURL URLWithString:@"https://www.tumblr.com/oauth/authorize"];
OAMutableURLRequest* authorizeRequest = [[OAMutableURLRequest alloc] initWithURL:authorizeUrl
consumer:nil
token:nil
realm:nil
signatureProvider:nil];
NSString* oauthToken = requestToken.key;
OARequestParameter* oauthTokenParam = [[OARequestParameter alloc] initWithName:@"oauth_token" value:oauthToken] ;
[authorizeRequest setParameters:[NSArray arrayWithObject:oauthTokenParam]];
// UIWebView* webView = [[UIWebView alloc] initWithFrame:[UIScreen mainScreen].bounds];
// webView.scalesPageToFit = YES;
// [[[UIApplication sharedApplication] keyWindow] addSubview:webView];
// webView.delegate = self;
[self.WebView loadRequest:authorizeRequest];
}
#pragma mark UIWebViewDelegate
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
NSLog(@"scheme: %@",[[request URL] scheme]);
if ([[[request URL] scheme] isEqualToString:@"tumblr"]) {
// 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://www.tumblr.com/oauth/access_token"];
OAMutableURLRequest* accessTokenRequest = [[OAMutableURLRequest alloc] initWithURL:accessTokenUrl
consumer:consumer
token:requestToken
realm:nil
signatureProvider:nil];
OARequestParameter* verifierParam = [[OARequestParameter alloc] initWithName:@"oauth_verifier" value:verifier];
[accessTokenRequest setHTTPMethod:@"POST"];
[accessTokenRequest setParameters:[NSArray arrayWithObject:verifierParam]];
OADataFetcher* dataFetcher = [[OADataFetcher alloc] init];
[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 {
NSLog(@"webView error: %@",error);
// ERROR!
}
- (void)webViewDidStartLoad:(UIWebView *)webView {
[spinner setHidden:NO];
[spinner startAnimating];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
[spinner setHidden:YES];
[spinner stopAnimating];
}
- (void)didReceiveAccessToken:(OAServiceTicket*)ticket data:(NSData*)data {
NSString* httpBody = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
accessToken = [[OAToken alloc] initWithHTTPResponseBody:httpBody];
NSString *OAuthKey = accessToken.key; // HERE YOU WILL GET ACCESS TOKEN
NSString *OAuthSecret = accessToken.secret; //HERE YOU WILL GET SECRET TOKEN
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:@"Tumblr Token"
message:OAuthSecret
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alertView show];
}
一切正常,但登录后
if ([[[request URL] scheme] isEqualToString:@"tumblr"]) {
}
应该在 shouldStartLoadWithRequest 委托方法中调用
但不满足给定条件。以至于我无法验证 oauth_verifier 并且无法获取 accessToken。
请指教谢谢
您必须使用 https 而不是 http 因为 Tumblr.It 的每个 url 都能完美运行。
谢谢。
我正在通过 oauth 使用 tumbler 登录身份验证。我正在关注这个 url:http://codegerms.com/login-with-tumblr-in-uiwebview-using-xcode-6-part-3/ 用于登录身份验证,并通过在 UIWebview 中登录,在 iOS 中获取 Tumblr API 的访问令牌和密钥。
我正在使用这段代码。
- (void)viewDidLoad {
[super viewDidLoad];
// clientID = @"Tjta51N6kF6Oxmm1f3ytpUvMPRAE1bRgCgG90SOa0bJMlSlLeT";
// secret = @"lrlQPNx3Yb1nRxp4qreYXvUURkGUmYCBoQacOmLTDRJAc7awRN";
clientID = @"sdF0Y6bQoJYwfIB1Mp7WECwobAgnq5tmkRjo7OXyKHDg3opY7Y";
secret = @"qJNGrRjyriZBeBhcgJz0MAcD9WAYXUW1tLbLrbYE4ZclzAUH9g";
redirect = @"tumblr://authorized";
[self.WebView setBackgroundColor:[UIColor clearColor]];
[self.WebView setOpaque:NO];
[self connectTumblr];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)connectTumblr {
consumer = [[OAConsumer alloc]initWithKey:clientID secret:secret];
NSURL* requestTokenUrl = [NSURL URLWithString:@"http://www.tumblr.com/oauth/request_token"];
OAMutableURLRequest* requestTokenRequest = [[OAMutableURLRequest alloc] initWithURL:requestTokenUrl
consumer:consumer
token:nil
realm:nil
signatureProvider:nil] ;
OARequestParameter* callbackParam = [[OARequestParameter alloc] initWithName:@"oauth_callback" value:redirect] ;
[requestTokenRequest setHTTPMethod:@"POST"];
[requestTokenRequest setParameters:[NSArray arrayWithObject:callbackParam]];
OADataFetcher* dataFetcher = [[OADataFetcher alloc] init] ;
[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];
requestToken = [[OAToken alloc] initWithHTTPResponseBody:httpBody];
NSURL* authorizeUrl = [NSURL URLWithString:@"https://www.tumblr.com/oauth/authorize"];
OAMutableURLRequest* authorizeRequest = [[OAMutableURLRequest alloc] initWithURL:authorizeUrl
consumer:nil
token:nil
realm:nil
signatureProvider:nil];
NSString* oauthToken = requestToken.key;
OARequestParameter* oauthTokenParam = [[OARequestParameter alloc] initWithName:@"oauth_token" value:oauthToken] ;
[authorizeRequest setParameters:[NSArray arrayWithObject:oauthTokenParam]];
// UIWebView* webView = [[UIWebView alloc] initWithFrame:[UIScreen mainScreen].bounds];
// webView.scalesPageToFit = YES;
// [[[UIApplication sharedApplication] keyWindow] addSubview:webView];
// webView.delegate = self;
[self.WebView loadRequest:authorizeRequest];
}
#pragma mark UIWebViewDelegate
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
NSLog(@"scheme: %@",[[request URL] scheme]);
if ([[[request URL] scheme] isEqualToString:@"tumblr"]) {
// 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://www.tumblr.com/oauth/access_token"];
OAMutableURLRequest* accessTokenRequest = [[OAMutableURLRequest alloc] initWithURL:accessTokenUrl
consumer:consumer
token:requestToken
realm:nil
signatureProvider:nil];
OARequestParameter* verifierParam = [[OARequestParameter alloc] initWithName:@"oauth_verifier" value:verifier];
[accessTokenRequest setHTTPMethod:@"POST"];
[accessTokenRequest setParameters:[NSArray arrayWithObject:verifierParam]];
OADataFetcher* dataFetcher = [[OADataFetcher alloc] init];
[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 {
NSLog(@"webView error: %@",error);
// ERROR!
}
- (void)webViewDidStartLoad:(UIWebView *)webView {
[spinner setHidden:NO];
[spinner startAnimating];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
[spinner setHidden:YES];
[spinner stopAnimating];
}
- (void)didReceiveAccessToken:(OAServiceTicket*)ticket data:(NSData*)data {
NSString* httpBody = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
accessToken = [[OAToken alloc] initWithHTTPResponseBody:httpBody];
NSString *OAuthKey = accessToken.key; // HERE YOU WILL GET ACCESS TOKEN
NSString *OAuthSecret = accessToken.secret; //HERE YOU WILL GET SECRET TOKEN
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:@"Tumblr Token"
message:OAuthSecret
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alertView show];
}
一切正常,但登录后
if ([[[request URL] scheme] isEqualToString:@"tumblr"]) {
}
应该在 shouldStartLoadWithRequest 委托方法中调用 但不满足给定条件。以至于我无法验证 oauth_verifier 并且无法获取 accessToken。 请指教谢谢
您必须使用 https 而不是 http 因为 Tumblr.It 的每个 url 都能完美运行。 谢谢。