没有使用Facebook 4.4.0 SDK获取电子邮件和公开个人资料 [英] Not getting Email and public profile using Facebook 4.4.0 SDK

查看:211
本文介绍了没有使用Facebook 4.4.0 SDK获取电子邮件和公开个人资料的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用Facebook SDK 4.4.0。 4.4.0版本之前,在4.3.0中,我们收到了以下代码的public_profile电子邮件:

  NSArray * arrFBPermission = [[ NSArray alloc] initWithObjects:@email,@public_profile,nil]; 
[loginUsingFB logInWithReadPermissions:arrFBPermission handler:^(FBSDKLoginManagerLoginResult * result,NSError * error){
[[FBSDKGraphRequest alloc] initWithGraphPath:@me参数:nil] startWithCompletionHandler:^(FBSDKGraphRequestConnection * id result,NSError * error){
if(!error){
NSString * fnm = [result valueForKey:@first_name];
NSString * lnm = [result valueForKey:@last_name];
}
else {
Nslog(%@,error localizedDescription);
}
}];
}

现在在新的SDK(即4.4.0)我没有得到这些值。为什么?我想要从Facebook的电子邮件,first_name,last_name,id。

解决方案

使用Facebook 4.4 SDK有一点点变化。
您必须使用您想从Facebook帐户的 FBSDKGraphRequest 请求参数。



您的代码中有 nil 参数:



使用以下参数更新代码:

  [[FBSDKGraphRequest alloc] initWithGraphPath:@me参数:@ {@fields:@id,name,link,first_name, last_name,picture.type(large),email,birthday,bio,location,friends,hometown,friendlists}] 

如果要使用自定义按钮登录Facebook,则可以使用完整的代码如下:

   - (IBAction)btnFacebookPressed:(id)sender {
FBSDKLoginManager * login = [[FBSDKLoginManager alloc] init];
login.loginBehavior = FBSDKLoginBehaviorBrowser;
[login logInWithReadPermissions:@ [@email] handler:^(FBSDKLoginManagerLoginResult * result,NSError * error)
{
if(error)
{
/ /进程错误
}
else if(result.isCancelled)
{
//处理取消
}
else
{
if([result.grantedPermissions containsObject:@email])
{
NSLog(@result is:%@,result);
[self fetchUserInfo];
[login logOut]; // Only如果您不想保存当前应用程序的会话
}
}
}];
}
- (void)fetchUserInfo
{
if([FBSDKAccessToken currentAccessToken])
{
NSLog(@令牌可用:%@ ,[[FBSDKAccessToken currentAccessToken] tokenString]);

[[FBSDKGraphRequest alloc] initWithGraphPath:@me参数:@ {@fields:@id,name,link,first_name,last_name,picture.type(large),email }]
startWithCompletionHandler:^(FBSDKGraphRequestConnection * connection,id result,NSError * error){
if(!error)
{
NSLog(@resultis:%@,结果);

}
else
{
NSLog(@Error%@,error);
}
}];
}
}

我希望它能为您服务。 >

I am currently working on Facebook SDK 4.4.0. Before 4.4.0 version, in 4.3.0 we were getting email, public_profile with the following code

NSArray *arrFBPermission = [[NSArray alloc]initWithObjects:@"email",@"public_profile", nil];
    [loginUsingFB logInWithReadPermissions:arrFBPermission handler:^(FBSDKLoginManagerLoginResult *result, NSError *error){ 
[[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:nil] startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error){
                      if (!error){
                          NSString *fnm = [result valueForKey:@"first_name"];
                          NSString *lnm = [result valueForKey:@"last_name"];
                      }
else{
     Nslog("%@", error localizedDescription);
}
                  }];
}

Now in the New SDK (i.e. 4.4.0) I am not getting these values. Why? I want email, first_name, last_name, id from Facebook.

解决方案

With Facebook 4.4 SDK there is a slight change. You must have to request the parameter with the FBSDKGraphRequest which you want from Facebook account.

In your code there is nil in parameters :

Update your code with the parameters like as :

[[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:@{@"fields": @"id, name, link, first_name, last_name, picture.type(large), email, birthday, bio ,location , friends ,hometown , friendlists"}]

If you want to login with Facebook with the use of Custom Button then you can use the complete code as follows :

- (IBAction)btnFacebookPressed:(id)sender {
    FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
    login.loginBehavior = FBSDKLoginBehaviorBrowser;
    [login logInWithReadPermissions:@[@"email"] handler:^(FBSDKLoginManagerLoginResult *result, NSError *error)
     {
         if (error)
         {
             // Process error
         }
         else if (result.isCancelled)
         {
             // Handle cancellations
         }
         else
         {
             if ([result.grantedPermissions containsObject:@"email"])
             {
                 NSLog(@"result is:%@",result);
                 [self fetchUserInfo];
                 [login logOut]; // Only If you don't want to save the session for current app
             }
         }
     }];
}
-(void)fetchUserInfo
{
    if ([FBSDKAccessToken currentAccessToken])
    {
        NSLog(@"Token is available : %@",[[FBSDKAccessToken currentAccessToken]tokenString]);

        [[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:@{@"fields": @"id, name, link, first_name, last_name, picture.type(large), email"}]
         startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
             if (!error)
             {
                 NSLog(@"resultis:%@",result);

             }
             else
             {
                 NSLog(@"Error %@",error);
             }
         }];
    }
}

I hope it will work for you.

这篇关于没有使用Facebook 4.4.0 SDK获取电子邮件和公开个人资料的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆