2013-02-28 59 views
0

我添加了在我的應用中將圖片上傳到Flickr的功能。我使用ObjectiveFlickr並使用SnapAndRun示例代碼,並使其工作。唯一的問題是,它看起來很醜陋。試圖清理iOS OAuth代碼

我很少做很多網絡編碼,所以這有點難以理解。

使用OAuth授權我的應用程序似乎通過如此多的功能進行了線索...必須有一個更清晰的方式。

我想過使用塊,但我沒有很好的處理它。

我基本上有一個5步的過程,在一個函數中開始,將一些東西發送到Flickr,它在另一個函數中返回並調用另一個函數,並且在......上...請求令牌......等待答覆,建立一個webview確認...得到另一個令牌...更多的等待...並最終擊中我的上傳代碼。

有沒有人做過這種更清潔的方式?我會喜歡一個小方向。

非常感謝。

回答

2

我面臨同樣的問題與tumblr進行身份驗證。我發現使用oauth-consumer的解決方案。您可以從gitgoogle-group下載。

你必須在閃存API中註冊here,你可以從那裏得到CONSUMER_KEYCONSUMER_SECRET

#define CONSUMER_KEY @"your consumer key here" 
#define CONSUMER_SECRET @"your secret key here" 
#define authorize_url @"http://www.flickr.com/services/oauth/authorize" 
#define request_token_url @"http://www.flickr.com/services/oauth/request_token" 

-(void)btnPressed:(id)sender{ 
    self.consumer = [[OAConsumer alloc] initWithKey:CONSUMER_KEY secret:CONSUMER_SECRET]; 

    NSURL *url = [NSURL URLWithString:request_token_url]; 


    OAMutableURLRequest *request = [[OAMutableURLRequest alloc] initWithURL:url 
                    consumer:self.consumer 
                     token:nil // we don't have a Token yet 
                     realm:nil // our service provider doesn't specify a realm 
                  signatureProvider:nil]; // use the default method, HMAC-SHA1 

    [request setHTTPMethod:@"POST"]; 

    OADataFetcher *fetcher = [[OADataFetcher alloc] init]; 

    [fetcher fetchDataWithRequest:request 
         delegate:self 
       didFinishSelector:@selector(requestTokenTicket:didFinishWithData:) 
        didFailSelector:@selector(requestTokenTicket:didFailWithError:)]; 
} 

- (void)requestTokenTicket:(OAServiceTicket *)ticket didFinishWithData:(NSData *)data { 
    if (ticket.didSucceed) 
    { 
     NSString *responseBody = [[NSString alloc] initWithData:data 
                 encoding:NSUTF8StringEncoding]; 
     self.accessToken= [[OAToken alloc] initWithHTTPResponseBody:responseBody]; 

     NSURL *author_url = [NSURL URLWithString:[ NSString stringWithFormat:authorize_url,self.accessToken.key]]; 
     OAMutableURLRequest *oaR = [[OAMutableURLRequest alloc] initWithURL:author_url consumer:nil token:nil realm:nil signatureProvider:nil]; 
     UIWebView *webView =[[UIWebView alloc] initWithFrame:[UIScreen mainScreen].bounds]; 
     [[[UIApplication sharedApplication] keyWindow] addSubview:webView]; 
     webView.delegate=self; 
     [webView setScalesPageToFit:YES]; 
     [webView loadRequest:oaR]; 

    } 
} 

- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType { 

    NSString *url = [[request URL] absoluteString]; 
    NSString *keyOne = @"oauth_token"; 
    NSString *keyTwo = @"oauth_verifier"; 
    NSRange r1 =[url rangeOfString:keyOne]; 
    NSRange r2 =[url rangeOfString:keyTwo]; 
    if (r1.location!=NSNotFound && r2.location!=NSNotFound) { 
     // 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:@"http://www.tumblr.com/oauth/access_token"]; 
      OAMutableURLRequest* accessTokenRequest =[[OAMutableURLRequest alloc] initWithURL:accessTokenUrl 
                        consumer:self.consumer 
                         token:self.accessToken 
                         realm:nil 
                      signatureProvider:nil]; 
      OARequestParameter* verifierParam =[[OARequestParameter alloc] initWithName:@"oauth_verifier" value:verifier]; 
      [accessTokenRequest setHTTPMethod:@"POST"]; 
      [accessTokenRequest setParameters:[NSArray arrayWithObjects:verifierParam,nil]]; 
      OADataFetcher* dataFetcher = [[OADataFetcher alloc] init]; 
      [dataFetcher fetchDataWithRequest:accessTokenRequest 
            delegate:self 
          didFinishSelector:@selector(requestTokenTicketForAuthorization:didFinishWithData:) 
           didFailSelector:@selector(requestTokenTicket:didFailWithError:)]; 
     } else { 
      // ERROR! 
     } 
     [webView removeFromSuperview]; 
     return NO; 
    } 
    if([url isEqualToString:@"http://www.elegantmedia.com.au/"]){ 
     [webView removeFromSuperview]; 
     return NO; 
    } 
    return YES; 
} 


- (void)requestTokenTicketForAuthorization:(OAServiceTicket *)ticket didFinishWithData:(NSData *)data 
{ 
    if (ticket.didSucceed) 
    { 
     NSString *responseBody = [[NSString alloc] initWithData:data 
                 encoding:NSUTF8StringEncoding]; 
     self.accessToken = [self.accessToken initWithHTTPResponseBody:responseBody]; 
     accessTokenKey=self.accessToken.key; 
     accessTokenSecret=self.accessToken.secret; 
     //[self post]; 

     NSString *blogUrl=(NSString *)[[NSUserDefaults standardUserDefaults]objectForKey:@"tumblrBlogUrl"]; 
     if([blogUrl isEqualToString:@""]||blogUrl==nil){ 
      [self getUserInfo]; 
     } 
     else{ 
      NSData *data = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"picture" ofType:@"jpg"]]; 
      [self postPhoto:data caption:@"Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when" inBlog:blogUrl]; 
     } 
    } 
    else 
    { 
     NSString *responseBody = [[NSString alloc] initWithData:data 
                 encoding:NSUTF8StringEncoding]; 
     NSLog(@"Response = %@",responseBody); 
    } 


} 
- (void)requestTokenTicket:(OAServiceTicket *)ticket didFailWithError:(NSError *)error 
{ 
    NSLog(@"Error = %@",[error localizedDescription]); 
} 

如果需要任何額外的參數,你可以從here

+0

THanks。我會試試這個。它已經看起來更清潔了。 – pizzafilms 2013-02-28 15:45:30

+0

您的慷慨代碼發佈中缺少一些屬性或實例變量。我可以使用一點幫助。謝謝。 – pizzafilms 2013-02-28 23:55:45