0

我已成功提示用户通过 SocialFramework 使用以下方法授予 facebook 权限,但似乎无法检索并显示基本个人资料信息(姓名、电子邮件、身份证等......)我想有简单方法,但找不到它们。任何人都可以在这里提供一些帮助吗?谢谢

-(IBAction)getInfo:(id)sender{

NSLog(@"FIRING");

ACAccountStore *_accountStore = [[ACAccountStore alloc] init];

ACAccountType *facebookAccountType = [_accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];

// We will pass this dictionary in the next method. It should contain your Facebook App ID key,
// permissions and (optionally) the ACFacebookAudienceKey
NSDictionary *options = @{ACFacebookAppIdKey : @"284395038337404",
ACFacebookPermissionsKey : @[@"email"],
ACFacebookAudienceKey:ACFacebookAudienceOnlyMe};

// Request access to the Facebook account.
// The user will see an alert view when you perform this method.
[_accountStore requestAccessToAccountsWithType:facebookAccountType
                                       options:options
                                    completion:^(BOOL granted, NSError *error) {
                                        if (granted)
                                        {
                                            NSLog(@"GRANTED");
                                            // At this point we can assume that we have access to the Facebook account
                                            NSArray *accounts = [_accountStore accountsWithAccountType:facebookAccountType];

                                            // Optionally save the account
                                            [_accountStore saveAccount:[accounts lastObject] withCompletionHandler:nil];
                                        }
                                        else
                                        {
                                            NSLog(@"Failed to grant access\n%@", error);
                                        }
                                    }];

}

4

1 回答 1

2

使用您发布的代码,您只能访问您获得的帐户的非常基本的信息,如屏幕名称或描述,在这种情况下是 facebook...

一个例子:

ACAccount *account = [accounts lastObject];
NSString *username = account.username;

为了获得更完整的信息,如真实姓名、电子邮件等,您需要使用 graph facebook api 的 /me 功能进行 SLRequest。

NSURL *requestURL = [NSURL URLWithString:@"https://graph.facebook.com/me"];
NSString *serviceType = SLServiceTypeFacebook;

NSOperationQueue *queue = [[NSOperationQueue alloc] init];

[queue setName:@"Perform request"];
[queue addOperationWithBlock:^{

    SLRequest *request = [SLRequest requestForServiceType:serviceType requestMethod:SLRequestMethodGET URL:requestURL parameters:nil];

    [request setAccount:account];

    NSLog(@"Token: %@", account.credential.oauthToken);

    [request performRequestWithHandler:^(NSData *data, NSHTTPURLResponse *response, NSError *error) {

        // Handle the response...
        if (error) {
            NSLog(@"Error: %@", error);
            //Handle error
        }
        else {

            NSDictionary* jsonResults = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];

            if (error) {

                NSLog(@"Error: Error serializating object");
                //Handle error
            }
            else {

                    NSDictionary *errorDictionary = [jsonResults valueForKey:@"error"];

                    if (errorDictionary) {

                        NSNumber *errorCode = [errorDictionary valueForKey:@"code"];

                        //If we get a 190 code error, renew credentials
                        if ([errorCode isEqualToNumber:[NSNumber numberWithInt:190]]) {

                            NSLog(@"Renewing credenciales...");

                            [self.accountStore renewCredentialsForAccount:account completion:^(ACAccountCredentialRenewResult renewResult, NSError *error){

                                if (error) {
                                    NSLog(@"Error: %@", error);
                                    //Handle error
                                }
                                else {
                                    if (renewResult == ACAccountCredentialRenewResultRenewed) {
                                        //Try it again
                                    }
                                    else {
                                        NSLog(@"Error renewing credenciales...");

                                        NSError *errorRenewengCredential = [[NSError alloc] initWithDomain:@"Error reneweng facebook credentials" code:[errorCode intValue] userInfo:nil];

                                        if (renewResult == ACAccountCredentialRenewResultFailed) {
                                            //Handle error
                                        }
                                        else if (renewResult == ACAccountCredentialRenewResultRejected) {
                                            //Handle error
                                        }
                                    }
                                }
                            }];
                        }
                    }
                    else {
                        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                            NSLog(@"jsonResults: %@", jsonResults);
                        }];
                    }
                }
            }
        }
    }];
}];

此代码还检查错误的可能性,如果令牌已过期并在后台运行网络进程以不阻塞接口,则进行一些处理。

您将在 jsonResults 字典中找到所有信息,您可以通过以下方式访问:

NSString *name = [jsonResults objectForKey:@"first_name"];
NSString *lastName = [jsonResults objectForKey:@"last_name"];
NSString *email = [jsonResults objectForKey:@"email"];

查看 facebook 文档以获取更多信息: https ://developers.facebook.com/docs/reference/api/user/

希望能帮助到你!

于 2012-10-15T08:24:16.493 回答