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
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.
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];
}
}];
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;
}
}];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With