简体   繁体   English

Facebook Graph API获取用户的个人资料照片

[英]Facebook Graph API Fetching User's Profile Photo

I'm using the Facebook Graph API v2.0. 我正在使用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. 在2012年或更早的时候,提出了类似的问题,但此后API发生了变化。 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. 我尝试了其他几种选择,例如自己启动连接并使用诸如“ / me?fields = picture”之类的图形路径,但是它们都导致相同的错误。

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) {

            }
}];

For the new Facebook Graph API I use: 对于新的Facebook Graph API,我使用:

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 您可以在此处了解有关用户数据的更多信息

借助此URL模板加载个人资料图片

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM