【问题标题】:unable to get the access token through [FBSDKAccessToken currentAccessToken] in iphone sdk无法通过 iphone sdk 中的 [FBSDKAccessToken currentAccessToken] 获取访问令牌
【发布时间】:2015-07-28 06:35:42
【问题描述】:

我正在创建一个 Facebook 应用程序。我已经使用类 FBSDKLoginManager 实现了登录。第一次登录 facebook 时,成功块返回 FBSDKLoginManagerLoginResult 对象,其中包含 访问令牌对象(FBSDKAccessToken 对象),该对象用于共享包含。但在第二次(重新启动应用程序后)当我尝试使用函数 [FBSDKAccessToken currentAccessToken] 获取访问令牌时返回 nil。那么如何在不再次登录facebook的情况下第二次获取访问令牌(FBSDKAccessToken对象)

【问题讨论】:

  • 它应该在重启后跟踪访问令牌,所以这是意外行为。你确定你第一次登录正确吗?
  • 第二次保存FBSDKAccessToken的解决方案你得到了吗?我也面临这个问题

标签: ios objective-c iphone facebook facebook-graph-api


【解决方案1】:

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

AppDalegate.m

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
    return [[FBSDKApplicationDelegate sharedInstance] application:application
                                                          openURL:url
                                                sourceApplication:sourceApplication
                                                       annotation:annotation];
}

ViewController.m

- (IBAction)btnFacebookPressed:(id)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];
             }
         }
     }];
}

-(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);
             }
         }];

    }

}

viewDidLoad调用此方法登录后获取访问令牌

[self fetchUserInfo];

【讨论】:

  • :: 不错的答案 +1... 但无法获取好友列表、家乡、朋友、位置、生日等...您能帮帮我吗??
  • @BandishDave 请参阅 www.developer.facebook.com,因为 SDK 发生了许多变化。如果您的朋友通过 Facebook 登录使用您的应用程序,那么您只能获得这些朋友。
【解决方案2】:

你需要打电话

[[FBSDKApplicationDelegate sharedInstance] application:application didFinishLaunchingWithOptions:launchOptions]

之前

[FBSDKAccessToken currentAccessToken]

【讨论】:

    【解决方案3】:

    试试这个代码。基于 Facebook SDK 6.0 及以上版本

    - (IBAction)fbIntegration:(id)sender
    {
    
        FBSDKLoginManager *loginmanager = [[FBSDKLoginManager alloc] init];
    
        [loginmanager logInWithReadPermissions:@[@"email",@"public_profile"] fromViewController:Nil handler:^(FBSDKLoginManagerLoginResult *result, NSError *error)
    
         {
             if (error)
             {
                 // Process error
                 NSLog(@"======error%@",error);
             }
             else if (result.isCancelled)
             {
                 // Handle cancellations
                 NSLog(@"canceled");
             }
             else
             {
                 if ([result.grantedPermissions containsObject:@"email"])
                 {
                     NSLog(@"result is:%@",result);
                     [self fetchUserInfo];
                 }
             }
         }];
    
    }
    -(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(@"%@",connection);
                  }
                 else
                 {
                     NSLog(@"Error %@",error);
                 }
             }];
    
        }
    
    }
    

    希望这对其他人有所帮助。

    【讨论】:

      【解决方案4】:

      如果您在模拟器上尝试您的应用程序,请在真机上尝试,因为模拟器存在用户默认错误。

      Falling back to loading access token from NSUserDefaults because of simulator bug

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-07-30
        • 2013-02-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多