Authenticate TWRequest for Streaming API

mjrichards91
@mjrichards91 Mike Richards

am looking to use the Twitter Streaming API in iOS5 by using TWReqeust. The request is a a POST request. This is the code I am using:

  1.     ACAccountStore *store = [[ACAccountStore alloc] init];
  2. ACAccountType *twitterAccountType = [store accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
  3.  
  4. [store requestAccessToAccountsWithType:twitterAccountType withCompletionHandler:^(BOOL granted, NSError *error)
  5. {
  6.     if (!granted){
  7.         NSLog(@"User rejected access to his account.");
  8.     } else {
  9.         //Grab the available accounts
  10.         NSArray *twitterAccounts = [store accountsWithAccountType:twitterAccountType];
  11.  
  12.         if([twitterAccounts count] > 0)
  13.         {
  14.             //Use the first account for simplicity
  15.             ACAccount *account = [twitterAccounts objectAtIndex:0];
  16.  
  17.             //Authenticated request
  18.             NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
  19.             [params setObject:@"1" forKey:@"include_entities"];
  20.  
  21.             //Endpoint to call
  22.             NSURL *url = [NSURL URLWithString:@"https://stream.twitter.com/1/statuses/filter.json?track=winning"];
  23.  
  24.             TWRequest *request = [[TWRequest alloc] initWithURL:url parameters:params requestMethod:TWRequestMethodPOST];
  25.  
  26.             //Attach account object to request
  27.             [request setAccount:account];
  28.  
  29.             NSURLRequest *twitterRequest = [request signedURLRequest];
  30.  
  31.             NSURLConnection *twitterConnection = [[NSURLConnection alloc] initWithRequest:twitterRequest delegate:self startImmediately:NO];
  32.  
  33.             [twitterConnection scheduleInRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
  34.             [twitterConnection start];
  35.  
  36.             if (twitterConnection)
  37.             {
  38.                 NSLog(@"Connection!!");
  39.             } else {
  40.                 NSLog(@"NO Connection!!");
  41.             }
  42.         }
  43.     }
  44. }]; 

I am getting a response and a data object. However when I convert the data object into a string I get this response:

  1.  Data that is received: <html>
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  4. <title>Error 401 Unauthorized</title>
  5. </head>
  6. <body>
  7. <h2>HTTP ERROR: 401</h2>
  8. <p>Problem accessing '/1/statuses/filter.json?track=winning'. Reason:
  9. <pre>    Unauthorized</pre>                                           
  10. </body>
  11. </html> 

I am not sure why the request is unauthorized since I am using the account from the account store. Any idea what I am doing wrong?

4 days 13 hours ago