OGeek|极客世界-中国程序员成长平台

标题: ios - 尝试登录 iOS sdk 时无法通过 FBSDKLoginKit 获取访问 token [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 10:45
标题: ios - 尝试登录 iOS sdk 时无法通过 FBSDKLoginKit 获取访问 token

我正在使用 facebook sdk 在 facebook 上共享文本,但在尝试登录时无法获取访问 token 。登录成功,但没有获得任何访问 token 。 因此,如果没有访问 token ,我将无法在 facebook 上分享文本。 我的代码如下

FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
[login logInWithReadPermissions[@"public_profile"] handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {


if ([result.token hasGranted"publish_actions"]) {
    [[[FBSDKGraphRequest alloc]
      initWithGraphPath"me/feed"
      parameters: @{ @"message" : @"hello world"}
      HTTPMethod"OST"]
     startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
         if (!error) {
             NSLog(@"ost id:%@", result[@"id"]);
         }
     }];
}

if (error) {
    // Process error
} else if (result.isCancelled) {

    // Handle cancellations
} else {
    // If you ask for multiple permissions at once, you
    // should check if specific permissions missing
    if ([result.grantedPermissions containsObject"email"]) {
        // Do work
    }
}

}];

我在 FBSDKLoginManager 对象的处理程序 block 中得到结果,没有错误,但它不包含 token 或其他数据

输出如下

enter image description here

请告诉我如何解决这个问题 谢谢



Best Answer-推荐答案


试试这个代码。基于 Facebook SDK 4.0 版

AppDalegate.m

- (BOOL)applicationUIApplication *)application openURLNSURL *)url sourceApplicationNSString *)sourceApplication annotationid)annotation {
    return [[FBSDKApplicationDelegate sharedInstance] application:application
                                                          openURL:url
                                                sourceApplication:sourceApplication
                                                       annotation:annotation];
}

ViewController.m

- (IBAction)btnFacebookPressedid)sender {
    FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
    [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];
             }
         }
     }];
}

-(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, birthday, bio ,location ,friends ,hometown , friendlists"}]
         startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
             if (!error)
             {
                 NSLog(@"resultis:%@",result);
             }
             else
             {
                 NSLog(@"Error %@",error);
             }
         }];

    }

}

关于ios - 尝试登录 iOS sdk 时无法通过 FBSDKLoginKit 获取访问 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30248048/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4