Michael Wu
Michael Wu

Reputation: 1277

Facebook Graph API Fetching User's Profile Photo

I'm using the Facebook Graph API v2.0. The following call

[FBRequestConnection startWithGraphPath:@"/me/picture" completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
    if (!error) {
        NSLog(@"%@", result);
    } else {
        NSLog(@"%@", [error userInfo]);
    }
}];

results in this error:

{
    NSLocalizedDescription = "Response is a non-text MIME type; endpoints that return images and other binary data should be fetched using NSURLRequest and NSURLConnection";
    "com.facebook.sdk:ErrorSessionKey" = "<FBSession: 0x10f036250, state: FBSessionStateOpen, loginHandler: 0x100073a40, appID: 863523550341327, urlSchemeSuffix: , tokenCachingStrategy:<FBSessionTokenCachingStrategy: 0x10f0339c0>, expirationDate: 4001-01-01 00:00:00 +0000, refreshDate: 2014-07-27 17:20:14 +0000, attemptedRefreshDate: 0001-12-30 00:00:00 +0000, permissions:(\n    installed,\n    \"public_profile\",\n    email,\n    \"user_birthday\",\n    \"user_location\",\n    \"user_friends\"\n)>";
    "com.facebook.sdk:HTTPStatusCode" = 0;
}

Does anyone know why this is happening? All I really need is a response containing a url to my profile picture. In 2012 and earlier, similar questions were asked but the API has changed since then. Please don't mark this as a duplicate unless you're actually sure that the question you redirect me to has a working solution. I've tried a couple of other alternatives such as starting the connection myself and using a graph path such as "/me?fields=picture" but they all result in the same error.

Upvotes: 1

Views: 1549

Answers (3)

Alvaro
Alvaro

Reputation: 286

For the new Facebook Graph API I use:

FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc]
                              initWithGraphPath:@"/me?fields=id,name,picture" //As many fields as you need
                              parameters:nil
                              HTTPMethod:@"GET"];
[request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection,
                                      id result,
                                      NSError *error) {
    if (!error){
        NSDictionary *picture = [(NSDictionary*)result objectForKey:@"picture"];
        NSDictionary *data = [picture objectForKey:@"data"];
        NSString *url = [data objectForKey:@"url"];
        NSData *dataImage = [NSData dataWithContentsOfURL:[NSURL URLWithString:url]];

    }
}];

You can more info about user data here

Upvotes: 1

Michael Wu
Michael Wu

Reputation: 1277

It seems my original call requests an image. To get a text based response that gives a url to the profile photo, the following worked for me:

 [FBRequestConnection startWithGraphPath:@"me?fields=picture.height(500),picture.width(500)"completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
            if (error) {

            }
}];

Upvotes: 1

nerowolfe
nerowolfe

Reputation: 4817

Load profile picture with the help of this URL template

    NSString *imgUrl = [NSString stringWithFormat:@"http://graph.facebook.com/%@/picture", user.id];

Upvotes: 0

Related Questions