Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get twitter profile picture in ios?

I wrote the following code:

NSURL *url = [NSURL URLWithString:@"http://api.twitter.com/1.1/users/show.json"];

NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:username, @"screen_name" ,[[controller.engine accessToken] secret]];

TWRequest *request = [[TWRequest alloc] initWithURL:url parameters:params requestMethod:TWRequestMethodGET];

[request performRequestWithHandler:
 ^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
     if (responseData) {
         NSDictionary *user = [NSJSONSerialization JSONObjectWithData:responseData
                                         options:NSJSONReadingAllowFragments
                                           error:NULL];

         NSString *profileImageUrl = [user objectForKey:@"profile_image_url"];

         NSLog(@"%@",profileImageUrl);
     }
 }];

But I always end up getting a Bad authentication error. I feel I'm missing something. Would anyone please check my code? Or provide any suggestions to retrieve twitter user profile pictures?

Thanks

like image 260
Romit M. Avatar asked Sep 20 '13 13:09

Romit M.


People also ask

Why is my Twitter profile picture not showing up?

Try either upgrading your browser so it is up to date, or try using a different browser. Your upload problem may be related to the browser or computer you're using. Make sure you click 'Apply.


2 Answers

Have you considered using a 3rd party Twitter engine for this? I've used FHSTwitterEngine with a fair amount of success, and it seems to be under active development.

To pull a profile picture you would do something like this:

[[FHSTwitterEngine sharedEngine]permanentlySetConsumerKey:@"<consumer_key>" andSecret:@"<consumer_secret>"];
[[FHSTwitterEngine sharedEngine]showOAuthLoginControllerFromViewController:self
withCompletion:^(BOOL success) {
    if (success) {
        UIImage *profileImg = [[FHSTwitterEngine sharedEngine] getProfileImageForUsername:@"<username>" andSize:size];
    }
}];
like image 151
alexgophermix Avatar answered Oct 30 '22 08:10

alexgophermix


this is what i have tried in past

[PFTwitterUtils logInWithBlock:^(PFUser *user, NSError *error) {
    if (!user) {
        NSLog(@"Uh oh. The user cancelled the Twitter login.");
        [[NSNotificationCenter defaultCenter] postNotificationName:notificationUserLoginFailed
                                                            object:error];
        return;
    } else {

        // TODO find a way to fetch details with Twitter..

        NSString * requestString = [NSString stringWithFormat:@"https://api.twitter.com/1.1/users/show.json?screen_name=%@", user.username];


        NSURL *verify = [NSURL URLWithString:requestString];
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:verify];
        [[PFTwitterUtils twitter] signRequest:request];
        NSURLResponse *response = nil;
        NSData *data = [NSURLConnection sendSynchronousRequest:request
                                             returningResponse:&response
                                                         error:&error];


        if ( error == nil){
            NSDictionary* result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
            _NSLog(@"%@",result);

            [user setObject:[result objectForKey:@"profile_image_url_https"]
                     forKey:@"picture"];
            // does this thign help?
            [user setUsername:[result objectForKey:@"screen_name"]];

            NSString * names = [result objectForKey:@"name"];
            NSMutableArray * array = [NSMutableArray arrayWithArray:[names componentsSeparatedByString:@" "]];
            if ( array.count > 1){
                [user setObject:[array lastObject]
                         forKey:@"last_name"];

                [array removeLastObject];
                [user setObject:[array componentsJoinedByString:@" " ]
                         forKey:@"first_name"];
            }

            [user saveInBackground];
        }

        [[NSNotificationCenter defaultCenter] postNotificationName:notificationUserDidLogin
                                                            object:nil];

        return;
    }



}];
like image 38
Agent Chocks. Avatar answered Oct 30 '22 09:10

Agent Chocks.