0

アプリで Flickr に画像をアップロードする機能を追加しています。私は ObjectiveFlickr と SnapAndRun のサンプル コードを使用しており、動作しています。唯一の問題は、見た目がとても醜いことです。

私はめったにネットワーキングのコーディングをしないので、これを理解するのは少し難しいです。

OAuth を使用したアプリの承認は、非常に多くの機能を介してスレッド化されているようです...もっとクリーンな方法が必要です。

ブロックを使おうと思ったのですが、うまく扱えません。

私は基本的に、1つの関数で開始し、Flickrにいくつかのものを送信し、Flickrは別の関数に戻り、別の関数を呼び出し、そして何度も...トークンを要求し、返信を待って、webviewを表示する5つのステップのプロセスを持っています確認...別のトークンを取得しています...さらに待っています...そして最後にアップロードコードをヒットしました。

誰かがこれをよりクリーンな方法で行いましたか? 少し方向性が欲しいです。

本当にありがとう。

4

2 に答える 2

2

tumblrで認証するのと同じ問題に直面しています。そして、oauth-consumer を使用して解決策を見つけました。gitまたはgoogle-groupからダウンロードできます。

ここでフリッカーAPIに登録する必要がCONSUMER_KEYあり、そこから取得できますCONSUMER_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]);
}

追加のパラメーターが必要な場合は、ここから取得できます。

于 2013-02-28T08:03:00.323 に答える