【问题标题】:Facebook Graph API Fetching User's Profile PhotoFacebook Graph API 获取用户的个人资料照片
【发布时间】:2014-07-27 17:38:11
【问题描述】:

我正在使用 Facebook Graph API v2.0。以下调用

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

导致此错误:

{
    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;
}

有人知道为什么会这样吗?我真正需要的只是一个包含我个人资料图片网址的回复。在 2012 年及更早的时候,也有人问过类似的问题,但 API 从那时起发生了变化。请不要将此标记为重复,除非您确实确定将我重定向到的问题有一个有效的解决方案。我尝试了其他几种替代方法,例如自己启动连接并使用“/me?fields=picture”之类的图形路径,但它们都会导致相同的错误。

【问题讨论】:

    标签: objective-c facebook-graph-api ios7 facebook-graph-api-v2.0


    【解决方案1】:

    看来我最初的通话要求提供图片。要获得基于文本的响应,该响应为个人资料照片提供 url,以下内容对我有用:

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

    【讨论】:

      【解决方案2】:

      对于我使用的新 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]];
      
          }
      }];
      

      您可以了解更多关于用户数据的信息here

      【讨论】:

        【解决方案3】:

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

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

        【讨论】:

        • 我会这样做,但 api 可能会发生变化,这似乎不太模块化。
        猜你喜欢
        • 1970-01-01
        • 2014-06-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-04-16
        • 1970-01-01
        • 1970-01-01
        • 2011-09-24
        相关资源
        最近更新 更多