簡體   English   中英

Facebook Graph API獲取用戶的個人資料照片

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

我正在使用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”之類的圖形路徑,但是它們都導致相同的錯誤。

看來我的原始通話要求提供圖片。 為了獲得基於文本的回復,該回復提供了個人資料照片的網址,以下內容對我有用:

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

            }
}];

對於新的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]];

    }
}];

您可以在此處了解有關用戶數據的更多信息

借助此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