未使用 Facebook 4.4.0 SDK 获取电子邮件和公开个人资料 [英] Not getting Email and public profile using Facebook 4.4.0 SDK
问题描述
我目前正在开发 Facebook SDK 4.4.0.在 4.4.0 版本之前,在 4.3.0 中我们收到了带有以下代码的电子邮件 public_profile
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);
}
}];
}
现在在新 SDK(即 4.4.0)中我没有得到这些值.为什么?我想要来自 Facebook 的电子邮件、名字、姓氏和 ID.
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.
推荐答案
在 Facebook 4.4 SDK 中有一个细微的变化.您必须使用 FBSDKGraphRequest
从 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.
在您的代码中,参数中有 nil
:
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"}]
如果您想使用自定义按钮登录 Facebook,那么您可以使用如下完整代码:
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);
}
}];
}
}
希望它对你有用.
这篇关于未使用 Facebook 4.4.0 SDK 获取电子邮件和公开个人资料的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!