Login with Tumblr to UIWebView

I am using tumbler login authorization via oauth. I am following this url: http://codegerms.com/login-with-tumblr-in-uiwebview-using-xcode-6-part-3/ to authenticate login and get access token and secret key for Tumblr API on iOS via login in UIWebview.

I am using this block of code.

- (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];




}

      

Every thing works fine, but after entering it

if ([[[request URL] scheme] isEqualToString:@"tumblr"]) {
}

      

should call the shouldStartLoadWithRequest delegate method but this condition is not met. so i can't check oauth_verifier and couldn't get accessToken. Please, help.

+3


source to share


1 answer


You have to use https instead of http for every Tumblr.It url will work fine. Thank.



0


source







All Articles