在iPhone上使用Twitter + OAuth进行推文搜索

时间:2011-05-20 01:05:02

标签: iphone oauth twitter twitter-oauth

最初我需要能够在Twitter上使用搜索API。我用Matt Gemmell的伟大MGTwitterEngine做到了这一点。该代码非常简单,看起来像这样:

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

然后我可以用searchResults做我想做的事。这要求我包含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 个答案:

答案 0 :(得分:1)

问题是你必须将twitter引擎实例化为SA_OAuthTwitterEngine的实例,而不是MGTwitterEngine。当您调用getSearchResultsForQuery时,它使用对_sendRequestWithMethod的调用来实际发送搜索请求。 SA_OAuthTwitterEngine会覆盖MGTwitterEngine的实现,但它的该函数版本不适用于getSearchResultsForQuery。

因此,您需要转到getSearchResultsForQuery并使其使用MGTwitterEngine的函数版本。