ios中的Tumblr回调函数

时间:2016-05-25 05:40:58

标签: ios callback tumblr

我是ios的初学者。我刚刚开始在ios中集成tumblr app。 我需要你的帮助,因为我陷入了实施的困境。我想登录tumblr并返回我的viewcontroller。为此,我正在关注this link

我已经实现了代码,现在我正在获取tumblr的webview。但是,我在回到我的视图控制器时遇到了问题,并且它也从任何ID登录,即它不考虑CLientID&我提到的秘密。

这是我的代码。

-(void)viewDidLoad 
{
  [super viewDidLoad];
  clientID = @"pLD4s********************************";
  secret = @"ALJIE8X********************************";
  redirect = @"tumblr://authorized";

  [self connectTumblr];
}
- (void)viewWillAppear:(BOOL)animated 
{
  webView.delegate = self;
}
- (void)connectTumblr 
{
  consumer = [[OAConsumer alloc]initWithKey:clientID secret:secret];

  NSURL* requestTokenUrl = [NSURL URLWithString:@"https://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]];

  [webView loadRequest:authorizeRequest];
}

- (void)didFailOAuth:(OAServiceTicket*)ticket error:(NSError*)error
{
  NSLog(@"OAuth Fail");
}

- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType 
{
  if ([[[request URL] scheme] isEqual:@"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 isEqual:@"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)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];
  }

CLientID&秘密,我在注册后从tumblr取得enter code here

谢谢。

1 个答案:

答案 0 :(得分:1)

我建议你把断点放在几个地方&检查登录流程并给我们提供错误消息

  1. shouldStartLoadWithRequest
  2. if(verifier){
  3. [dataFetcher fetchDataWithRequest:accessTokenRequest委托:self didFinishSelector:@selector(didReceiveAccessToken:data :)      didFailSelector:@selector(didFailOAuth:错误:)];

  4. didReceiveAccessToken

  5. didFailOAuth

  6. 我想你错过了错误的方法。就是这样。

    - (void)didFailOdatah:(OAServiceTicket*)ticket error:(NSError*)error {
        // ERROR!
    }