title: Using tweet search with Twitter+OAuth on iPhone
body: <p>Originally I needed the ability to use the search API with twitter. I did this using Matt Gemmell's great <a href="http://mattgemmell.com/2008/02/22/mgtwitterengine-twitter-from-cocoa" rel="nofollow">MGTwitterEngine</a>. That code was very very simple and looked something like this:</p>
  
  <pre><code>- (void)viewDidLoad {
      [super viewDidLoad];
  
      tweetArrays = nil;
      tweetNameArray = nil;
  
      NSString *username = @"&lt;username&gt;";
      NSString *password = @"&lt;password&gt;";
  
      NSString *consumerKey = @"&lt;consumerKey&gt;";
      NSString *consumerSecret = @"&lt;consumerSecret&gt;";
  
      // 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];
  }
  </code></pre>
  
  <p>This would end up calling the function:</p>
  
  <pre><code>- (void)searchResultsReceived:(NSArray *)searchResults forRequest:(NSString *)connectionIdentifier
  </code></pre>
  
  <p>And then I could do what I wanted with the searchResults. This required me to include the yajl library.</p>
  
  <p>I then wanted to expand my code to allow users to tweet. I downloaded Ben Gottlieb's great code <a href="https://github.com/bengottlieb/Twitter-OAuth-iPhone" rel="nofollow">Twitter-OAuth-iPhone</a></p>
  
  <p>So there's only one problem. The getSearchResultsForQuery returns a requestFailed with the following error:</p>
  
  <pre><code>Error Domain=HTTP Code=400 "The operation couldn’t be completed. (HTTP error 400.)"
  </code></pre>
  
  <p>To call this code I simply took the demo project in Twitter-OAuth-iPhone and added a call to getSearchResultsForQuery as seen here:</p>
  
  <pre><code>- (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]]];
      }
  }
  </code></pre>
  
  <p>This as stated above returns a 400 error. Every other twitter API call I add here does work such as:</p>
  
  <pre><code>- (NSString *)getRepliesStartingAtPage:(int)pageNum;
  </code></pre>
  
  <p>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?</p>
  
  <p>Thanks!</p>