Jinwoo Hong
Jinwoo Hong

Reputation: 51

iPhone Facebook request Error

I request @"me", but requestDidLoadWithError method show this error.

An active access token must be used to query information about the current user.

I can't resolve this problem. Any solution?

- (IBAction)loginBtn:(id)sender 
{
facebook = [[Facebook alloc] initWithAppId:@"231190276934148" andDelegate:self];

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:@"FBAccessTokenKey"] && [defaults objectForKey:@"FBExpirationDateKey"]) {
    facebook.accessToken = [defaults objectForKey:@"FBAccessTokenKey"];
    facebook.expirationDate = [defaults objectForKey:@"FBExpirationDateKey"];
}

if (![facebook isSessionValid]) {
    NSArray *permissions = [[NSArray alloc] initWithObjects:
                            @"user_likes",
                            @"user_about_me",
                            nil];
    [facebook authorize:permissions];
}

[self fbDidLogin];

[facebook requestWithGraphPath:@"me" andDelegate:self];

}

Upvotes: 1

Views: 841

Answers (2)

Nilesh Kikani
Nilesh Kikani

Reputation: 2618

You have done every thing correct but not in correct flow.

Just follow like this formatted code.

- (void)viewDidLoad
{
[super viewDidLoad];

permissions = [[NSArray alloc] initWithObjects:@"read_stream", @"publish_stream",@"offline_access", nil];

facebook = [[Facebook alloc] initWithAppId:kAppId andDelegate:self];

}

-(IBAction)onClickLoginWithFb:(id)sender
{
if (![facebook isSessionValid]) {
    [ facebook authorize:permissions];
}
else {
    [facebook requestWithGraphPath:@"me" andDelegate:self];
}
}

- (void)fbDidLogin {
[appDelegate showLoadingView];
[facebook requestWithGraphPath:@"me" andDelegate:self];
// Save authorization information
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[facebook accessToken] forKey:@"FBAccessTokenKey"];
[defaults setObject:[facebook expirationDate] forKey:@"FBExpirationDateKey"];
[defaults synchronize];

}

- (void)fbDidLogout {

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[facebook invalidateSession];
[defaults removeObjectForKey:@"FBAccessTokenKey"];
[defaults removeObjectForKey:@"FBExpirationDateKey"];
[defaults synchronize];

}

- (void)request:(FBRequest *)request didReceiveResponse:(NSURLResponse *)response {
NSLog(@"received response");

}

- (void)request:(FBRequest *)request didLoad:(id)result {

if ([result isKindOfClass:[NSArray class]]) {
    result = [result objectAtIndex:0];
}

}

Upvotes: 1

mahmud
mahmud

Reputation: 90

You do not have to call [self fbDidLogin]; fbDidLogin is delegate method and will be called back when login will be successful.

Then move the code [facebook requestWithGraphPath:@"me" andDelegate:self]; only after successful login.

Upvotes: 0

Related Questions