Twitter Streaming Endpoint not working with SLRequest object

561 views Asked by At

I am trying to pull data from the Twitter Streaming API using the SLRequest class. When I use the endpoint and parameters documented in the code below the program "hangs" and no JSON data is printed. I am using an endpoint based on an example from the twitter dev website https://dev.twitter.com/docs/streaming-apis/parameters#with I am requesting tweets at a certain location.

When I use this code to query my timeline using the REST API (the code and request is included but commented out) the program does not hang and I get a valid response.

Is there something else in the code that I need to implement to access the data using the streaming API? What additional modifications or changes need to be made?

ACAccountStore * accountStore = [[ACAccountStore alloc] init];
ACAccountType * twitterAccountType =
[accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];

// Ask the user permission to access his account
[accountStore requestAccessToAccountsWithType:twitterAccountType options:nil completion:^(BOOL granted, NSError *error) {
    if (granted == NO) {
        NSLog(@"-- error: %@", [error localizedDescription]);
    }
    if (granted == YES){

       /***************** Create  request using REST API*********************
        ***************** This URL is functional and returns valid data *****
        NSURL * url = [NSURL URLWithString:@"https://userstream.twitter.com/1.1/user.json"];
         SLRequest * request = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodGET URL:url parameters:@{@"screen_name": @"your_twitter_id"}];
        ***************************************************************/

        // Create request using Streaming API Endpoint
        NSURL * url = [NSURL URLWithString:@"https://stream.twitter.com/1.1/statuses/filter.json"];

        NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
        [params setObject:@"track" forKey:@"twitter&"];
        [params setObject:@"locations" forKey:@"-122.75,36.8,-121.75,37.8"];

        SLRequest * request = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodPOST URL:url parameters:params];

        NSArray * twitterAccounts = [accountStore accountsWithAccountType:twitterAccountType];

        if ([twitterAccounts count] == 0) {
            (NSLog(@"-- no accounts available"));
        } else if ([twitterAccounts count] >0){
            [request setAccount:[twitterAccounts lastObject]];
            NSLog([request.account description]);
            NSLog(@"Twitter handler of user is %@", request.account.username);

            // Execute the request
            [request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
                NSError * jsonError = nil;
                NSDictionary *json = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingAllowFragments error:&jsonError];

                [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                    // NSLog(@"-- json Data is %@", json);
                    NSLog([json description]);
                }];

            }];

        }
    }

}];
1

There are 1 answers

0
nst On BEST ANSWER

SLRequest doesn't play well with the streaming API.

Here is how to do with STTwitter:

self.twitter = [STTwitterAPI twitterAPIOSWithAccount:account];

[_twitter verifyCredentialsWithSuccessBlock:^(NSString *username) {

    NSLog(@"-- access granted for %@", username);

    [_twitter postStatusesFilterUserIDs:nil
                        keywordsToTrack:@[@"twitter"]
                  locationBoundingBoxes:@[@"-122.75,36.8,-121.75,37.8"]
                              delimited:nil
                          stallWarnings:nil
                          progressBlock:^(id response) {
        NSLog(@"-- %@", response);
    } stallWarningBlock:^(NSString *code, NSString *message, NSUInteger percentFull) {
        NSLog(@"-- stall warning");
    } errorBlock:^(NSError *error) {
        NSLog(@"-- %@", [error localizedDescription]);
    }];

} errorBlock:^(NSError *error) {
    NSLog(@"-- %@", [error localizedDescription]);
}];

Internally, STTwitter builds a NSURLConnection instance with the request from -[SLRequest preparedURLRequest]. You can replicate this trick in your code if you wish.