Sign In Register

How can we help you today?

Start a new topic
Answered

FacebookConnectRequest

Should I call FacebookConnectRequest every time my game starts? Or only if the token is updated? Or not at all because it's handled on the GameSparks servers?


How does the expiry of the Facebook token get handled?


From the Facebook documentation:


"Native mobile apps using Facebook's SDKs will get long-lived access tokens, good for about 60 days. These tokens will be refreshed once per day when the person using your app makes a request to Facebook's servers. If no requests are made, the token will expire after about 60 days and the person will have to go through the login flow again to get a new token."


Does the token sent to the server on the initial FacebookConnectRequest get refreshed automatically? (perhaps to get an updated friends list once a day or something?


A Unity tutorial that contains optional Facebook login would be a great addition to the current documentation. I find it really hard to piece together what the correct and most efficient way is. It's all so opaque ;)


Best Answer

Hi Willem,


So if you have your state manager set up somewhat similar to below you would run this at the very beginning.


  

public void Awake()
        {
                Debug.Log("GS Manager Init...");
                if(!FB.IsInitialized)
                {
                        Debug.LogWarning("GS Manager| FB not initialized...");
                        FB.Init(FacebookLogin);
                }
                else
                {
                        FacebookLogin();
                }
 
                GS.GameSparksAvailable += HandleGameSparksAvailable;
 
        }

  

 

Then you would log into Facebook and assign a delegate to run when Facebook has logged in. In there you call the GS connection request.


  

void FacebookLogin ()
        {
                if(!FB.IsLoggedIn)
                {
                        FB.Login("", GS_LoginWithFacebook);
                }
        }
 
        void GS_LoginWithFacebook (FBResult _result)
        {
                if(FB.IsLoggedIn)
                {
                        new FacebookConnectRequest().SetAccessToken(FB.AccessToken).Send((response) => {
 
                                if(!response.HasErrors)
                                {
                                        Debug.Log("Gamesparks Logged in with FB...");
                                }
                                else
                                {
                                        Debug.LogWarning("FB Login Errrors...");
                                }
                       
                        });
                }
        }

  

 

Once the game has been authorized by the user on Facebook, the token will be retrieved automatically when required. We are currently in the process of overhauling our documentation and tutorial sections of the website and will be covering these topics in much more depth, for now I hope this helps you.


Thanks,

Liam



  


Answer

Hi Willem,


So if you have your state manager set up somewhat similar to below you would run this at the very beginning.


  

public void Awake()
        {
                Debug.Log("GS Manager Init...");
                if(!FB.IsInitialized)
                {
                        Debug.LogWarning("GS Manager| FB not initialized...");
                        FB.Init(FacebookLogin);
                }
                else
                {
                        FacebookLogin();
                }
 
                GS.GameSparksAvailable += HandleGameSparksAvailable;
 
        }

  

 

Then you would log into Facebook and assign a delegate to run when Facebook has logged in. In there you call the GS connection request.


  

void FacebookLogin ()
        {
                if(!FB.IsLoggedIn)
                {
                        FB.Login("", GS_LoginWithFacebook);
                }
        }
 
        void GS_LoginWithFacebook (FBResult _result)
        {
                if(FB.IsLoggedIn)
                {
                        new FacebookConnectRequest().SetAccessToken(FB.AccessToken).Send((response) => {
 
                                if(!response.HasErrors)
                                {
                                        Debug.Log("Gamesparks Logged in with FB...");
                                }
                                else
                                {
                                        Debug.LogWarning("FB Login Errrors...");
                                }
                       
                        });
                }
        }

  

 

Once the game has been authorized by the user on Facebook, the token will be retrieved automatically when required. We are currently in the process of overhauling our documentation and tutorial sections of the website and will be covering these topics in much more depth, for now I hope this helps you.


Thanks,

Liam



  

Hey Liam, 


Could you please help with understanding the following. By the practices of Facebook's OAuth2 an access token received by a mobile device is only a short lived one. Facebook suggests that the backend server using an extension call prolongs the expiration of the short lived access token. 


I also have a similar question as Willem. The tutorial makes it perfectly understandable how a short lived OAuth2 token is received and user is able to perform a Facebook & Gamesparks login. The questions that remain are: 

 - does Gamesparks automatically perform token extension call

 - if yes Faceobook gives a new token to backend servers with longer expiration date

 - how should a Facebook login be handled once the short lived token on clients side expires, and only the server has a valid long lived token?


Kind regards, Domen


 


1 person likes this

I've been doing what's suggested .... log into Facebook each launch of the App and then call FacebookConnectRequest() with the token that FB supplies, but whenever I call FB.LogInWithReadPermissions(Permissions, FacebookAuthCallback); then Facebook always pops up an annoying screen saying "Confirm Login" .... "You previously logged in to ..... with Facebook. Would you like to continue". Is this normal or do I need to set something up in my Facebook App page to allow a silent login once a player has approved the Facebook permissions (which are only "public_proflle" and "user_friends") ? I'm using Unity 5.6.4f1 and Facebook for Unity SDK 7.10.0 and this is happening on both my iOS and Android devices.



Hey Peter I'm new to gamesparks and I'm also using FB, iOS and Android, I would love to see your login/authentication code if possible, want to make sure I'm doing things right :S thanks

I have the exact same problem as Peter Wiseman, and the fix was to include the "client token" in the Facebook settings in Unity. You also need to enable "Native or desktop app" and "Is App Secret embedded in the client" in the advanced settings in the Facebook developer's site for your app. The client token is available on that same page.


I hope this helps someone :)

Update - this works great on Android, but on iOS the game was still bringing up the Facebook connect screen every time. This was because we were calling FB.LogInWithReadPermissions() every launch of the game. You're only supposed to do this once EVER unless the user actually logs out of Facebook. 


1 person likes this
Login to post a comment