iOS:使用自动登录获取Facebook访问令牌

时间:2015-12-26 04:02:22

标签: ios facebook facebook-graph-api parse-platform pffacebookutils

我在我的应用中使用Parse和Facebook进行帐户管理。每当用户通过Facebook按钮登录时,我的代码都能正常工作,因为facebook sdk会自动生成一个新的访问令牌。但是,如果我使用自动登录代码检查用户是否已批准该应用程序,我没有访问令牌,也无法访问该用户的Facebook数据。我不知道如何为已经同意使用我的应用程序的用户请求访问令牌。

正在加载数据:

let myGenericExtensionUsedForDouble : SomeArrayProtocol<Array<Double>> = SomeArrayProtocol(SomeArrayGenericExtensions())
let myGenericExtensionUsedForFloat : SomeArrayProtocol<Array<Float>> = SomeArrayProtocol(SomeArrayGenericExtensions())
// let myGenericExtensionUsedForInt : SomeArrayProtocol<Array<Int>> = SomeArrayProtocol(SomeArrayGenericExtensions()) // Error! Int not SomeType, OK!

var myDoubleArr = [10.1, 42, 15.8, 42.0, 88.3]
let my10EntriesOfTenDoubleArr = myGenericExtensionUsedForDouble.repeatNumberNumberManyTimes(10) // ten 10:s
let myFloatArr : Array<Float> = [1.3, 5, 8.8, 13.0, 28, 42.0, 42.002]
let myIntArr = [1, 2, 3]

let a = myGenericExtensionUsedForDouble.countNumberOf42s(myDoubleArr) // 2
let b = myGenericExtensionUsedForFloat.countNumberOf42s(myFloatArr) // 1

myDoubleArr = myGenericExtensionUsedForDouble.removeRandomElement(myDoubleArr) // [10.1, 15.8, 42.0, 88.3]

自动登录系统:

- (void)_loadData :(BOOL)updateData
{
    NSLog(@"entered _loadData");
    NSMutableDictionary* userInfoParams = [NSMutableDictionary dictionary];
    [userInfoParams setValue:@"id,name,email,gender" forKey:@"fields"];

    if([FBSDKAccessToken currentAccessToken])
    {
        NSLog(@"Expiration date of token: %@", [[FBSDKAccessToken currentAccessToken] expirationDate]);
    }
    else NSLog(@"No access token");


    FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:userInfoParams];
    [request startWithCompletionHandler:^(FBSDKGraphRequestConnection* connection, id result, NSError* error)
     {
         if(!error)
         {
             ...
         }
      }
}

此外,当用户点击登录按钮时我收到的访问令牌持续4周,所以如果有办法手动分配访问令牌,那么我可以这样做,但 (void)viewDidAppear:(BOOL)animated { if ([PFUser currentUser] || [PFFacebookUtils isLinkedWithUser:[PFUser currentUser]]) { NSLog(@"Yes"); [self _loadData:YES]; [self transitionToLoginSegue:(id)self]; } else NSLog(@"No"); } 不可分配所以我也不能这样做。

1 个答案:

答案 0 :(得分:0)

这是我的代码,

 if ([self check_network])
      {

      START_LOAD;
      FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
      [login
       logInWithReadPermissions:  @[@"public_profile",@"email",@"user_friends"]
       fromViewController:self
       handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
             if (error) {
                   STOP_LOAD;
                   NSLog(@"Process error");
                   NSLog(@"%@",error);
                   TOAST_FOR_TRY_AGAIN;
                   /*
                   UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Please Try Again"
                                                                 message:nil
                                                                delegate:self
                                                       cancelButtonTitle:@"Ok"
                                                       otherButtonTitles: nil];
                   [alert show];
                    */
             } else if (result.isCancelled)
             {
                    STOP_LOAD;
                   NSLog(@"Cancelled");
             } else
             {
                   NSLog(@"Logged in");
                   NSLog(@"Result=%@",result);

                   [[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:@{ @"fields": @"id,first_name,middle_name,last_name,name,picture,email"}]
                    startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error)
                    {
                          NSLog(@"Facebook result=%@",result);
                         if (!error)
                             {
                                [NSUSER setObject:result forKey:@"user_info"];
                                [NSUSER setObject:[result objectForKey:@"name"] forKey:@"skip_name"];
                                [NSUSER synchronize];

                                API_CALL_ALLOC(ls_apicall);
                                [ls_apicall Login: STRING([result objectForKey:@"id"])];

                          } else {
                                 STOP_LOAD;
                                NSLog(@"An error occurred getting friends: %@", [error localizedDescription]);
                          }
                    }];
             }
       }];
      }
      else
      {
            TOAST(TOAST_NETWORK_ERROR);
      }