1

I need to get a Twitter feed of a public account. I am able to get my own Twitter feed using the following code:

-(void) testTwitter {
{
    ACAccountStore *accountStore = [[ACAccountStore alloc]init];

    if (accountStore != nil)
    {
        ACAccountType *accountType = [accountStore     accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
        if (accountType != nil)
        {
            [accountStore requestAccessToAccountsWithType:accountType options:nil completion:^(BOOL granted, NSError *error)
             {
                 if (granted)
                 {

                     //Succesful Access
                     NSArray *twitterAccounts = [accountStore accountsWithAccountType:accountType];
                     if (twitterAccounts != nil)
                     {
                         ACAccount *currentAccount = [twitterAccounts objectAtIndex:0];
                         if (currentAccount != nil)
                         {
                             NSString *paraString = [NSString stringWithFormat:@"qlx_corp"];
                             NSDictionary *parameters = [NSDictionary dictionaryWithObject:paraString forKey:@"username"];

                             NSString *requestString = @"https://api.twitter.com/1.1/statuses/user_timeline.json";
                             SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodGET URL:[NSURL URLWithString:requestString] parameters:parameters];
                             [request setAccount:currentAccount];

                             [request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error)
                              {
                                  if ((error == nil) && ([urlResponse statusCode] == 200))
                                  {
                                      NSArray *twitterFeed = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:nil];
                                      NSDictionary *firstPost = [twitterFeed objectAtIndex:0];

                                      NSLog(@"firstPost = %@", [firstPost description]);
                                  }
                                  else {
                                      NSLog(@"error: %@",error);
                                  }
                              }];
                         }
                     }
                 }
                 else
                 {
                     //Access Denied
                     NSLog(@"access denied");
                 }
             }];
        }
    }

}
}

But when I substitute the following code for getting another account besides my own, I get this error:

code = 215;

message = "Bad Authentication data.";

And this is the code I substituted for a Successful Access (noted in the comments in posted code):

NSURL* url = [NSURL URLWithString:@"https://api.twitter.com/1.1/statuses/user_timeline.json"];
                     NSDictionary* params = @{@"count" : @"10", @"screen_name" : @"qlx_corp"};

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

                     [request performRequestWithHandler:^(NSData *responseData,
                                                          NSHTTPURLResponse *urlResponse, NSError *error) {

                         if (!error) {
                             NSError *jsonError;
                         NSArray *responseJSON = [NSJSONSerialization
                                                  JSONObjectWithData:responseData
                                                  options:NSJSONReadingAllowFragments
                                                  error:&jsonError];
                             NSLog(@"response json: %@",responseJSON);
                         // Show the content of the responseJSON in a view.
                     }
                      }];

What am I doing wrong that I am getting this error message and not the public twitter feed that I am requesting. My app asks for permission for Twitter use and it has been granted, so what could be the problem?

MSU_Bulldog
  • 3,501
  • 5
  • 37
  • 73
  • Did you try this: http://stackoverflow.com/questions/28498038/fetching-twitter-public-feeds-as-json/28663071#28663071 ? – Teja Nandamuri Aug 31 '15 at 19:33
  • 1
    Well I'm not using the Twitter API, I'm using Apple's Social framework. So I was trying to avoid using a different technique, but if I can't find a solution then that will be a good place to look for an alternative. Thanks! – MSU_Bulldog Aug 31 '15 at 19:45

1 Answers1

0

I ended up using the Twitter API, since now they support an application only mode that does not require user authentication to display a public Twitter feed. Refer to this link for some more information: Twitter OAuth and accessToken to GET statuses/user_timeline in Objective-C

Community
  • 1
  • 1
MSU_Bulldog
  • 3,501
  • 5
  • 37
  • 73