How to achieve what I want using Facebook?

Rewriting this post after spending a few more hours exploring things.

I’m trying to accomplish a few simple things by using Facebook that I can’t seem to get working:

  1. I want to store the player’s score on Facebook so that I can use Facebook’s Score API to maintain a leaderboard in the game. Setting the player’s score requires “publish_actions” permissions and the leaderboard requires “user_friends” permissions. I cannot request both of these in a single popup and I don’t want to request the publish permissions after every game if the player hasn’t given me those permissions. If I’m not able to do this, then likely I’ll have to drop the facebook leaderboard all together and only use local high scores. I would hate to have to do this. Is there any way to do this or should I just give up and only ask for publish permissions during the situation described below?

  2. I would like there to be a button for players to share their score to Facebook that provides a link back to the game’s page on my website which will autoredirect to the appropriate store on mobile, or just show the page on a normal browser. I’ve looked into Open Graph Stories and a regular news feed post. I’m not sure how to do either one. When I use the code below nothing happens. What is the best way to achieve this?

if facebook and gui.pick_node(fbpost, action.x, action.y) then
	local data = { caption="Check out my score in Pie Squares", description="I just scored " .. self.score .. "points in Pie Squares.", link="http://facebook.com/PieSquares"}
	facebook.show_dialog("feed", data, function(postid) end) 
end

Thanks!

  1. You cannot request both read and write permissions at the same time. You need to first do a facebook.login_with_read_permissions() and request user_friends. Then you can do a facebook.login_with_publish_permissions() and request publish_actions. Note that if the user has already granted the requested permissions nothing will happen. The user will not get a login dialog every time.
    Note that this rule applies not only for Defold. It’s the same for all Facebook SDKs a far as I know.

  2. You want is for friends who doesn’t play the game to install it right? If that is the case then facebook.show_dialog(“apprequests”) is what you are looking for. "apprequests Shows a Game Request dialog. Game Requests allows players to invite their friends to play a game."

Thanks Britzl.

  1. I’d really prefer not having 2 dialogs but I guess that is just something to suffer through. I can’t nest the calls though because that causes the inner callback to not be called. Any thoughts on a good way to open the publish permissions after the read permissions AND then validate that all permissions were accepted afterwards without nested callbacks?

  2. I do want to do app requests, but I also just want to share a news feed post that just says “I scored XXXXX points!” I’ve tried doing it with the above but it does nothing with no indication of why. An implemented example would really be beneficial here (maybe in the tutorials or your public examples?).

Just a small note; If I recall correctly this is a limitation on from the Facebook SDK and/or a suggested flow from their part. :slight_smile:

I think I’ve figured out a way to split up the permissions that I like.

A couple more questions:

If the user deauthorizes the app, how can I detect that? I’ve tried checking the access token on startup (so that I can show the correct button), but it always seems to have a value even when I remove the app authorization from Facebook. The Javascript SDK seems to have a FB.getLoginStatus() function that would be useful in Defold. I can’t seem to find an equivalent in the graph API

Also if I’ve already granted permissions, it shows the pop up saying it has already done that. Can I avoid showing the popup in those situations? Checking the granted permissions has the similar problem as the access token as they are still filled out on startup even if the user has deauthorized the app in facebook.

Hello @codecloak!

At any point in the code you should be able to use facebook.permissions() to retrieve all the permissions the user has granted. This table is only updated when a new request is sent, so if a user grants permissions and then remove some of them outside of the application that will not be reflected in the table returned from facebook.permissions(). When you attempt to perform an action (such as get email) towards Facebook on the users behalf you will however be notified that you do not have that permission, and can take appropriate action.

Last time I checked, Facebook describes the best practise in terms of permissions to request all read permissions the first time the application starts, and then only request the minimum amount of required publish permissions just as the user is about to perform the action that require those permissions. You should be able to find a link to these guidelines in the Defold documentation of the API.

I would suggest that you check against facebook.permissions() before performing any action to determine whether you need to request the permissions or not, and that you are able to rerequest a missing permission, and then redo the action, in case of an unexpected failure due to the user revoking permissions.

Using facebook.loginWithReadPermission(...) multiple times for the same permission will transfer the user to the Facebook page or application multiple times, this should however be avoided using the method described above.

Edit: If we wanted the Facebook functionality in Defold to keep updated information about the current permission and login status for a user we would need to periodically request this information from Facebook using network requests. This would impact performance and even though we could use some heuristic to determine the likely best time within a frame to place such a request, it would always be wrong for some situations, scenarios, or games.

Regards,
Jakob

You should be able to iterate over the result of facebook.permissions() to ensure that the user has granted all the requested permissions.

Blossom Blast does facebook.login_with_read_permissions() on every app start if the user has chosen to Facebook connect the app. There is no popup saying something about already granted permissions. Do you have the Facebook app running on the phone and are you logged in with a valid user (ie one that has been added to your app in the Facebook developer dashboard, I assume it is not live yet?). Is this on Android or iOS?

Doh, Jakob beat me to it!

1 Like

I’ve tried iterating over facebook.permissions() on startup and the specific case I’m trying to handle is if the user deauthorizes without logging out through the app.

I’m running the app on the phone and I’m logged in with my personal account, which is the only valid user currently. Right now it is on Android.

Also about that other issue. The feed dialog still doesn’t seem to do anything when I’m running it with the code above. I would expect it to give me some sort of dialog allowing me to author a post.

@britzl, I’m quite certain that Blossom Blast either checks facebook.access_token(), facebook.permissions() or their own session value to determine whether to do a facebook.login_with_read_permissions(...) call or not.

Since we do not determine whether the list of known permissions is a superset of the list of requested permissions, we will always issue a call to Facebook. And since the user may have removed some permissions we do not know about, we cannot know if our list of permissions is correct to check for subsets.

Doing some checking, it seems like I can make something workable out of some Graph API requests that will accomplish what I need it to to avoid popups. I’ll have to try that in 12-13 hours when I get back from work.

Any suggestions on how to make the feed dialog work correctly?

Nope, if I’m reading the code correctly they actually do a facebook.login on each app start. But let’s debate the correctness of that when we’re both at the office :slight_smile: