Facebook dialog, logout and access token (Facebook iOS SDK)

10,968

I create the Facebook object in viewDidLoad

_facebook = [[Facebook alloc] init];

I have a tableView. When the user clicks on an entry, an action sheet pops up with 3 choices

1 send an email

2 share on Facebook

3 log out from Facebook: it should be there only if the user is logged, however for the moment it's there and it works. I use

[_facebook logout:self]; 

and moreover the method

- (void)fbDidLogout is called.

When the user clicks on "share on Facebook", this code is run

SBJSON *jsonWriter = [SBJSON new];
NSDictionary *actionLinks = [NSArray arrayWithObjects:
                                [NSDictionary dictionaryWithObjectsAndKeys:
                                            @"Some text", @"text",
                                        @"http://try.com",@"href", nil], nil];

NSString *actionLinksStr = [jsonWriter stringWithObject:actionLinks];

NSDictionary* attachment = [NSDictionary dictionaryWithObjectsAndKeys:
                                       @"is using...", @"name",
                                    @"It's an app….", @"caption",
                                    @"Download!", @"description",
                                    @"http://ituneslink", @"href",
                                    nil];

NSString *attachmentStr = [jsonWriter stringWithObject:attachment];
NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                       API_KEY, @"api_key",
                                       @"Message",  @"user_message_prompt",
                                       actionLinksStr, @"action_links",
                                       attachmentStr, @"attachment",nil];

        [_facebook dialog:@"stream.publish" andParams:params andDelegate:self];

        [jsonWriter release];

In viewDidLoad, I've also tried to use

_facebook.accessToken    = [[NSUserDefaults standardUserDefaults] stringForKey:@"AccessToken"];
_facebook.expirationDate = (NSDate *) [[NSUserDefaults standardUserDefaults] objectForKey:@"ExpirationDate"];

_permissions =  [[NSArray arrayWithObjects: @"read_stream", @"offline_access",nil] retain];

  if ([_facebook isSessionValid] == NO) {
      [_facebook authorize:API_KEY permissions:_permissions delegate:self];
  }

putting in fbDidLogin:

[[NSUserDefaults standardUserDefaults] setObject:_facebook.accessToken forKey:@"AccessToken"];
 [[NSUserDefaults standardUserDefaults] setObject:_facebook.expirationDate forKey:@"ExpirationDate"];

but fbDidLogin is never called.

EDIT

I resolved using the FB october api (while I've not found a solution with the november one).

Share:
10,968

Related videos on Youtube

Sefran2
Author by

Sefran2

Updated on June 04, 2022

Comments

  • Sefran2
    Sefran2 almost 2 years

    When a Facebook dialog is created with code like this

    NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys: apiKey, @"api_key", nil];
    [facebook dialog:@"stream.publish" andParams:params andDelegate:self];
    

    is there the possibility to use sessions?

    I succeeded in publishing a post on a user's post wall, but how allow the user to logout? Moreover I obtain this error when I try to access to current user information.

    {
        error = {
            message = "An active access token must be used to query information about the current user.";
            type = OAuthException;
        };
    }
    
  • Sefran2
    Sefran2 over 13 years
    How is possible that access token is null? I print it with NSLog(@"%s", facebook.accessToken); and it's value is null. So I can't retrieve current user info.
  • Sefran2
    Sefran2 over 13 years
    In NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys: apiKey, @"api_key", nil]; [facebook dialog:@"stream.publish" andParams:params andDelegate:self]; only the api_key is used. Where have I use the secret key?
  • Sefran2
    Sefran2 over 13 years
    I succeed in logging out with [facebook logout:self]; but as the accessToken returns always null I can't not only retrieve user info but also use a custom action sheet with "Log out from fb" entry only when it needs
  • WrightsCS
    WrightsCS over 13 years
    Your not going to be able to retrieve info if the user is logged out.
  • Sefran2
    Sefran2 over 13 years
    Sorry, I mean that the access token returns null also when the user is logged in and he/she can post on his/her wall. Now I'm trying to use [[NSUserDefaults standardUserDefaults] setObject:self.facebook.accessToken forKey:@"AccessToken"]; [[NSUserDefaults standardUserDefaults] setObject:self.facebook.expirationDate forKey:@"ExpirationDate"]; and restore them in the viewDidLoad but for the moment without success
  • WrightsCS
    WrightsCS over 13 years
    It may be a permissions issue, try reading over the docs: developers.facebook.com/docs/authentication/permissions
  • Sefran2
    Sefran2 over 13 years
    I've tried also with the permissions but nothing. I've noted that the fbDidLogin is never called. How is it possible, if the user can login and post?
  • Bill the Lizard
    Bill the Lizard about 13 years
    @Fran: In this case you should have edited this information into the question. Whatever you did later to "resolve some issues" should have been posted as an answer and accepted.
  • Sefran2
    Sefran2 about 13 years
    @Bill the Lizard: Thanks. But isn't there another problem (that is I've accepted an answer of mine)?
  • Bill the Lizard
    Bill the Lizard about 13 years
    @Fran: No, accepting your own answer when no other answer works is perfectly fine. You do need to make sure you actually answer the question though. This seems like a follow-up question. Am I right?
  • Sefran2
    Sefran2 about 13 years
    @ Bill the Lizard: actually, the above code works with the FB october api, but not with the last one (the november one).