无法通过iphone sdk中的[FBSDKAccessToken currentAccessToken]获取访问令牌

时间:2015-05-16 10:47:26

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

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

4 个答案:

答案 0 :(得分:22)

尝试此代码。基于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 :(得分:18)

您需要致电

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

<强>之前

[FBSDKAccessToken currentAccessToken]

答案 2 :(得分: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);
             }
         }];

    }

}

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

答案 3 :(得分:1)

如果您在模拟器上尝试应用程序,请尝试使用真实设备,因为模拟器有一个用户默认错误。

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