2011-05-20 94 views
0

最初我需要能夠使用twitter與搜索API。我用Matt Gemmell的偉大的MGTwitterEngine做了這個。該代碼是非常非常簡單,看起來是這樣的:在iPhone上使用Twitter上的Twitter + OAuth搜索

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    tweetArrays = nil; 
    tweetNameArray = nil; 

    NSString *username = @"<username>"; 
    NSString *password = @"<password>"; 

    NSString *consumerKey = @"<consumerKey>"; 
    NSString *consumerSecret = @"<consumerSecret>"; 

    // Most API calls require a name and password to be set... 
    if (! username || ! password || !consumerKey || !consumerSecret) { 
     NSLog(@"You forgot to specify your username/password/key/secret in AppController.m, things might not work!"); 
     NSLog(@"And if things are mysteriously working without the username/password, it's because NSURLConnection is using a session cookie from another connection."); 
    } 

    // Create a TwitterEngine and set our login details. 
    twitterEngine = [[MGTwitterEngine alloc] initWithDelegate:self]; 
    [twitterEngine setUsesSecureConnection:NO]; 
    [twitterEngine setConsumerKey:consumerKey secret:consumerSecret]; 
    // This has been undepreciated for the purposes of dealing with Lists. 
    // At present the list API calls require you to specify a user that owns the list. 
    [twitterEngine setUsername:username]; 

[twitterEngine getSearchResultsForQuery:@"#HelloWorld" sinceID:0 startingAtPage:1 count:100]; 
} 

這最終會調用該函數:

- (void)searchResultsReceived:(NSArray *)searchResults forRequest:(NSString *)connectionIdentifier 

然後,我可以做我想做與SearchResult所。這需要我包括yajl庫。

然後,我想擴展我的代碼,以允許用戶鳴叫。我下載了Ben Gottlieb的偉大代碼Twitter-OAuth-iPhone

所以只有一個問題。該getSearchResultsForQuery返回requestFailed,出現以下錯誤:

Error Domain=HTTP Code=400 "The operation couldn’t be completed. (HTTP error 400.)" 

要調用這個代碼,我只是參加了Twitter的OAuth的iPhone示範項目,並添加到getSearchResultsForQuery呼叫如下所示:

- (void) viewDidAppear: (BOOL)animated { 
    if (_engine) return; 
    _engine = [[SA_OAuthTwitterEngine alloc] initOAuthWithDelegate: self]; 
    _engine.consumerKey = kOAuthConsumerKey; 
    _engine.consumerSecret = kOAuthConsumerSecret; 

    UIViewController   *controller = [SA_OAuthTwitterController controllerToEnterCredentialsWithTwitterEngine: _engine delegate: self]; 

    if (controller) 
     [self presentModalViewController: controller animated: YES]; 
    else { 
     [_engine getSearchResultsForQuery:@"HelloWorld"]; 
     // [_engine sendUpdate: [NSString stringWithFormat: @"Already Updated. %@", [NSDate date]]]; 
    } 
} 

如上所述,返回400錯誤。我在此處添加的每個其他Twitter API函數都會執行以下操作:

- (NSString *)getRepliesStartingAtPage:(int)pageNum; 

我做錯了什麼?或者getSearchResultsForQuery不再有效?這兩個代碼庫似乎使用不同版本的MGTwitterEngine,可能會導致問題?

謝謝!

回答

1

問題是,您必須將twitter引擎實例化爲SA_OAuthTwitterEngine的實例,而不是MGTwitterEngine。當您調用getSearchResultsForQuery時,它會使用對_sendRequestWithMethod的調用來實際發送搜索請求。 SA_OAuthTwitterEngine覆蓋MGTwitterEngine的實現,但其函數版本不適用於getSearchResultsForQuery。

因此,您需要轉到getSearchResultsForQuery並使其使用MGTwitterEngine的該函數版本。