1

I am using MGTwitterEngine lib to authenticate Twitter account, but after successful login I want to get user profile pic but not able to get it and following method is stopped working:

https://api.twitter.com/1.1/users/profile_image?screen_name=username&size=bigger

I just tried above method but no success. Then I have did lot of research on it and I finally got following api to get user profile information which include profile pic:

https://api.twitter.com/1.1/users/show.json?screen_name=username

But this api requires authentication by passing Authorization in Request header which I was not able to generate.

Here is my code which I have tried:

here authdata is the string which I am getting back from MGTwitterEngine lib after successful login.

NSString *authData = [[NSUserDefaults standardUserDefaults] valueForKey:@"authData"];
NSString *subAuthdata = [authData substringFromIndex:[authData rangeOfString:@"oauth_token_secret="].location];
subAuthdata = [subAuthdata substringToIndex:[subAuthdata rangeOfString:@"&user_id="].location];
NSLog(@"%@", subAuthdata);

NSLog(@"encoded: %@", [self base64Encode:subAuthdata]);

NSString *accessTokenHeaderToPost = [NSString stringWithFormat:@"Basic %@", [self base64Encode:subAuthdata]];

NSString  *twitURL = @"https://api.twitter.com/1.1/users/show.json?screen_name=username";
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString: twitURL]];
[request setHTTPMethod:@"GET"];
[request setValue:accessTokenHeaderToPost forHTTPHeaderField:@"Authorization"];
NSError *err;
NSURLResponse *response;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];

NSLog (@"RESP: %@", responseString);

But it always says Bad authentication data.

Please help me on this...

  • Why not using the built-in twitter sdk? most users run ios>5.0 (about 98%), which has the twitter api (socialkit if I'm not wrong) built-in to iOS. – Lior Pollak Aug 01 '13 at 15:40

1 Answers1

10

Here is a solution using STTwitter:

STTwitterAPI *twitter = [STTwitterAPI twitterAPIAppOnlyWithConsumerKey:@"" consumerSecret:@""];

[twitter verifyCredentialsWithSuccessBlock:^(NSString *bearerToken) {

    [twitter getUsersShowForUserID:nil orScreenName:@"barackobama" includeEntities:nil successBlock:^(NSDictionary *user) {

        NSString *profileImageURLString = [user valueForKey:@"profile_image_url"];
        NSURL *url = [NSURL URLWithString:profileImageURLString];
        UIImage *profileImage = [UIImage imageWithContentsOfURL:url];

    } errorBlock:^(NSError *error) {
        //
    }];

} errorBlock:^(NSError *error) {
    //
}];
Aamir
  • 16,329
  • 10
  • 59
  • 65
nst
  • 3,862
  • 1
  • 31
  • 40
  • Comes back with low quality image, any way to get the original? – Kyle Greenlaw Aug 23 '14 at 08:14
  • @KyleGreenlaw just remove the "_normal" in the url to get the original full sized image. Here are some other options as per Twitter's guide https://dev.twitter.com/docs/user-profile-images-and-banners – Tim Sep 02 '14 at 23:39