开发者

Using tweet search with Twitter+OAuth on iPhone

Originally I needed the ability to use the search API with twitter. I did this using Matt Gemmell's great 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. This required me to include the yajl library.

I then wanted to expand my code to allow users to tweet. I downloaded Ben Gottlieb's great code Twitter-OAuth-iPhone

So there's only one problem. The getSearchResultsForQuery returns a requestFailed with the following error:

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:

- (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. Every other twitter API call I a开发者_Python百科dd here does work such as:

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

Am I doing anything wrong? Or does getSearchResultsForQuery no longer work? The two code bases seem to use different versions of MGTwitterEngine, could that be causing the problem?

Thanks!


The problem is that you have to instantiate the twitter engine as an instance of SA_OAuthTwitterEngine, instead of MGTwitterEngine. When you call getSearchResultsForQuery, it uses a call to _sendRequestWithMethod to actually send the search request. SA_OAuthTwitterEngine overrides the MGTwitterEngine's implementation, but its version of that function doesn't work with getSearchResultsForQuery.

So, you need to go to getSearchResultsForQuery and make it use the MGTwitterEngine's version of the function.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜