简体   繁体   English

在iPhone上的Twitter + OAuth中使用推文搜索

[英]Using tweet search with Twitter+OAuth on iPhone

Originally I needed the ability to use the search API with twitter. 最初,我需要能够在Twitter上使用搜索API。 I did this using Matt Gemmell's great MGTwitterEngine . 我使用Matt Gemmell的出色MGTwitterEngine做到了这一点 That code was very very simple and looked something like this: 该代码非常简单,看起来像这样:

- (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];
}

This would end up calling the function: 最终将调用该函数:

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

And then I could do what I wanted with the searchResults. 然后,我可以使用searchResults做我想做的事情。 This required me to include the yajl library. 这需要我包括yajl库。

I then wanted to expand my code to allow users to tweet. 然后,我想扩展我的代码以允许用户鸣叫。 I downloaded Ben Gottlieb's great code Twitter-OAuth-iPhone 我下载了Ben Gottlieb的出色代码Twitter-OAuth-iPhone

So there's only one problem. 因此,只有一个问题。 The getSearchResultsForQuery returns a requestFailed with the following error: getSearchResultsForQuery返回带有以下错误的requestFailed:

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

To call this code I simply took the demo project in Twitter-OAuth-iPhone and added a call to getSearchResultsForQuery as seen here: 要调用此代码,我只是在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]]];
    }
}

This as stated above returns a 400 error. 如上所述,这将返回400错误。 Every other twitter API call I add here does work such as: 我在此处添加的所有其他twitter API调用的工作方式如下:

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

Am I doing anything wrong? 我做错什么了吗? Or does getSearchResultsForQuery no longer work? 还是getSearchResultsForQuery不再起作用? The two code bases seem to use different versions of MGTwitterEngine, could that be causing the problem? 这两个代码库似乎使用了不同版本的MGTwitterEngine,这可能会引起问题吗?

Thanks! 谢谢!

The problem is that you have to instantiate the twitter engine as an instance of SA_OAuthTwitterEngine, instead of MGTwitterEngine. 问题是您必须将Twitter引擎实例化为SA_OAuthTwitterEngine的实例,而不是MGTwitterEngine。 When you call getSearchResultsForQuery, it uses a call to _sendRequestWithMethod to actually send the search request. 调用getSearchResultsForQuery时,它将使用对_sendRequestWithMethod的调用来实际发送搜索请求。 SA_OAuthTwitterEngine overrides the MGTwitterEngine's implementation, but its version of that function doesn't work with getSearchResultsForQuery. SA_OAuthTwitterEngine覆盖了MGTwitterEngine的实现,但是该函数的版本不适用于getSearchResultsForQuery。

So, you need to go to getSearchResultsForQuery and make it use the MGTwitterEngine's version of the function. 因此,您需要转到getSearchResultsForQuery并使其使用MGTwitterEngine的函数版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM