如何使用 Facebook Graph API 和 facebook ios sdk 获取完整的 User 对象?

发布于 2024-10-01 13:38:52 字数 2185 浏览 3 评论 0原文

我正在使用 Facebook iOS SDK 和 Facebook Graph API。当我请求“me”时(它应该返回当前用户的所有属性),我实际上只获得了 User 对象的一小部分。我做错了什么?

当我登录时,我也没有收到“允许/不允许”对话框,但这也许是因为我是应用程序所有者?

在我的标题中:

#import <UIKit/UIKit.h>
#import "Facebook.h"

@interface FacebookAppViewController : UIViewController <FBRequestDelegate, FBDialogDelegate, FBSessionDelegate> {
    Facebook* facebook;
    NSArray* permissions;

    IBOutlet UILabel* JSONLabel;
}

@property (retain, nonatomic) UILabel* JSONLabel;

- (IBAction)login:(id)sender;
- (IBAction)logout:(id)sender;

@end

在我的实现中:

#import "FacebookAppViewController.h"

static NSString* apiKey = @""; // with my app ID

@implementation FacebookAppViewController

@synthesize JSONLabel;

- (IBAction)login:(id)sender
{
    [facebook authorize:apiKey permissions:permissions delegate:self];
}

- (void) fbDidLogin
{
    [facebook requestWithGraphPath:@"me" andDelegate:self];
}

- (void)request:(FBRequest*)request didLoad:(id)result
{
    NSString* uid = [result objectForKey:@"id"];

    if ([result isKindOfClass:[NSDictionary class]])
    {
        NSString* text = @"";
        NSDictionary* hash = result;

        for (NSString* key in hash)
        {
            text = [text stringByAppendingFormat:@"\n%@: ", key];

            NSObject* value = [hash objectForKey:key];
            if (value == nil)
            {
                NSLog(@"value is nil");
                continue;
            }

            if ([value isKindOfClass:[NSString class]])
            {
                NSString* str = value;
                text = [text stringByAppendingFormat:@"%@", str];
            }
            JSONLabel.text = text;
        }
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];

    facebook = [[Facebook alloc] init];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
        permissions = [[NSArray arrayWithObjects:@"read_stream", @"offline_access", nil] retain];
    }
    return self;
}

I'm using the Facebook iOS SDK and Facebook Graph API. When I request "me", which should return all the properties of the current user, I'm only actually getting a small subset of the User object. What am I doing wrong?

I'm also not getting the Allow / Don't Allow dialog when I log in, but perhaps that is because I am the app owner?

In my header:

#import <UIKit/UIKit.h>
#import "Facebook.h"

@interface FacebookAppViewController : UIViewController <FBRequestDelegate, FBDialogDelegate, FBSessionDelegate> {
    Facebook* facebook;
    NSArray* permissions;

    IBOutlet UILabel* JSONLabel;
}

@property (retain, nonatomic) UILabel* JSONLabel;

- (IBAction)login:(id)sender;
- (IBAction)logout:(id)sender;

@end

In my implementation:

#import "FacebookAppViewController.h"

static NSString* apiKey = @""; // with my app ID

@implementation FacebookAppViewController

@synthesize JSONLabel;

- (IBAction)login:(id)sender
{
    [facebook authorize:apiKey permissions:permissions delegate:self];
}

- (void) fbDidLogin
{
    [facebook requestWithGraphPath:@"me" andDelegate:self];
}

- (void)request:(FBRequest*)request didLoad:(id)result
{
    NSString* uid = [result objectForKey:@"id"];

    if ([result isKindOfClass:[NSDictionary class]])
    {
        NSString* text = @"";
        NSDictionary* hash = result;

        for (NSString* key in hash)
        {
            text = [text stringByAppendingFormat:@"\n%@: ", key];

            NSObject* value = [hash objectForKey:key];
            if (value == nil)
            {
                NSLog(@"value is nil");
                continue;
            }

            if ([value isKindOfClass:[NSString class]])
            {
                NSString* str = value;
                text = [text stringByAppendingFormat:@"%@", str];
            }
            JSONLabel.text = text;
        }
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];

    facebook = [[Facebook alloc] init];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
        permissions = [[NSArray arrayWithObjects:@"read_stream", @"offline_access", nil] retain];
    }
    return self;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月寒剑心 2024-10-08 13:38:52

使用 fields 参数和任何记录的用户字段。
@“我?fields=id,姓名,性别,...”

Use the fields parameter and any of the documented user fields.
@"me?fields=id,name,gender,..."

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文