philippec / PhFacebook

MacOSX Interface to Facebook graph API
http://developer.casgrain.com/?p=107
Other
178 stars 44 forks source link

Post Request Error #36

Closed loretoparisi closed 11 years ago

loretoparisi commented 12 years ago

I integrated the sdk in a mac app. The user is logged is and the oAuth token is stored and cached. When I read the user profile everything is working fine, but the posting to Facebook via the open graph, it works only the first time after the initial login. The second time is not working anymore and no call to

is fired. If the user make the login again, and then post it's working fine.

The post call is something like that

[fb sendRequest:request params:params usePostRequest:YES];

when the params dictionary contains ordinary post fields and the request is a open graph path for the logged user (/me:appname:action).

Any idea?

loretoparisi commented 12 years ago

[UPDATE] It seems that this happens after re-opening the application, making a post without loggin in again. I initialized the SDK like this

fb = [[PhFacebook alloc] initWithApplicationID: @"xxxxxxxxxxxxx" delegate: self];

[fb getAccessTokenForPermissions: [NSArray arrayWithObjects: @"read_stream", @"publish_stream", @"publish_actions", @"offline_access", nil] cached: YES];

So I assume that the user has not a active session since nothing happens when I try to make a post. If I login again and then post, everything is working fine, since now the user has a valid session.

So, is the problem related to the facebook session being invalidated for some reason by the sdk ?

loretoparisi commented 12 years ago

[UPDATE] I figured out that the problem could be that I need to load the saved oAuth token in the application again. I tried this

fb = [[PhFacebook alloc] initWithApplicationID: @"xxxxxxxxxxxx" delegate: self];

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];

if( [prefs valueForKey:kMXMUserDefaultsFacebookAuthToken]) {
    NSString *token = (NSString*)[prefs valueForKey:kMXMUserDefaultsFacebookAuthToken];

     //////////  how to load the token in fb now       

  } 

The problem is that I don't have access to the "PhAuthenticationToken" from the framework. The method

needs some extra parameters to set a stored token.

So, how to set the stored access token in the "fb" object instance at startup?

I'm storing the access token as the following

loretoparisi commented 12 years ago

Wops closed for error!

philippec commented 12 years ago

I just added support for properly clearing cookies, allowing a complete logout. Does that help?

If the issue is you need access to the actual token, you can actually get it from the API itself: -accessToken

Does that help?

loretoparisi commented 12 years ago

Thanks for the new build, I'm testing it and I will let you know.

philippec commented 12 years ago

Note: I just pushed it right now

loretoparisi commented 11 years ago

Hi Philippe, not sure this problem was fixed, so writing here. If I login to FB:

fb = [[PhFacebook alloc] initWithApplicationID: FACEBOOK_APPID delegate: self];

and then ask for permissions:

[fb getAccessTokenForPermissions: [NSArray arrayWithObjects: @"read_stream", @"publish_actions", @"publish_stream", @"email", @"user_actions:musixmatch", @"friends_actions:musixmatch",nil] cached: YES];

everything works fine using the open graph requests.

When I close the app, and try a open graph request again, nothing happens.

Infact I check the accessToken here

if ( [fb accessToken] ) { [fb sendRequest:request params:params usePostRequest:NO]; }

the accessToken exists until the user is logged in and the app is running, while this does not exists when the app is restarted.

How to make the access token persistent inside the PhFacebook object ?

loretoparisi commented 11 years ago

So I finally came across this issue:

What I have to do is

at every application startup reload the access token inside the PhFacebook:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if([defaults valueForKey:kFBStoreAccessToken]) { // update access token
    NSString *accessToken = [defaults stringForKey: kFBStoreAccessToken];
    NSDate *date = [defaults objectForKey: kFBStoreTokenExpiry];
    NSString *perms = [defaults stringForKey: kFBStoreAccessPermissions];
    [fb setAccessToken:accessToken expires: [date timeIntervalSinceNow] permissions: perms error:nil];
} else { // no access token, need request new one

}

then every open graph request will work.