Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Unity SDK For Facebook - Login authentication popup does not appear.

Discussion in 'Scripting' started by TheCodingGuy23, Mar 13, 2019.

  1. TheCodingGuy23

    TheCodingGuy23

    Joined:
    Mar 6, 2019
    Posts:
    9
    Hi,

    I am currently experiencing problems with hosting my game on Facebook. I need to get user information (name, email, friends etc). This is what I have:

    Code (CSharp):
    1.  
    2. void LoginCallback(IResult result)
    3.     {
    4.         if (result.Error == null)
    5.         {
    6.             GameObject.Find("Name").GetComponent<Text>().text = result.ResultDictionary["name"].ToString();
    7.         }
    8.     }
    9.  
    10.     void AuthCallback(ILoginResult result)
    11.     {
    12.         if (FB.IsLoggedIn)
    13.         {
    14.             var aToken = AccessToken.CurrentAccessToken;
    15.  
    16.             FB.API("/me?name", HttpMethod.GET, LoginCallback);
    17.         }
    18.     }
    19.  
    20.     void InitializationCallback()
    21.     {
    22.         if (FB.IsInitialized)
    23.         {
    24.             FB.ActivateApp();
    25.  
    26.             FB.LogInWithReadPermissions(new List<string>() {"name"}, AuthCallback);
    27.         }
    28.     }
    29.  
    30.     void Awake()
    31.     {
    32.         if (!FB.IsInitialized)
    33.         {
    34.             FB.Init(InitializationCallback);
    35.         }
    36.         else
    37.         {
    38.             FB.ActivateApp();
    39.         }
    40.     }
    41.  
    This works perfectly fine in the editor. I am able to get user information based on the access token. When I host the game on Facebook, the login dialog box does not appear. Does anyone have a solution to this problem?
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,186
    When hosting on Facebook, there is no login box. The one in editor is a mock box. Usually it prompts for permissions, but if you already granted them, it isn't going to again.

    You can add some Debug.log statements and hit f12 in the browser (or open the browser developer console) and see your debug output if you need some confirmation on the values you are getting.
     
  3. TheCodingGuy23

    TheCodingGuy23

    Joined:
    Mar 6, 2019
    Posts:
    9
    It seems to stop execution at line 26. I get the following errors on the console:

     
    Last edited: Mar 14, 2019
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,186
    Is your game hosted with Facebook or hosted elsewhere and then just run on Facebook?
     
  5. TheCodingGuy23

    TheCodingGuy23

    Joined:
    Mar 6, 2019
    Posts:
    9
    I'm hosting on Facebook

    I think this has something to do with CORS. I've noticed that the error has been pointing to one of the JavaScript files (can't remember which one particularly). I will have another look tomorrow.
     
    Last edited: Mar 15, 2019
  6. TheCodingGuy23

    TheCodingGuy23

    Joined:
    Mar 6, 2019
    Posts:
    9
    The error that is being displayed on Google Chrome:
    I'm pretty sure this is a pop-up issue. I have tried disabling the pop-up feature in Chrome, but I still get the error.
     
  7. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,186
    I've never run into any issues on any browser I run with our game and they all have a popup blocker on them. I haven't done anything different with our main files. I have a modified template on one, but it just changes up images and the background behind the page.

    I usually just build, upload to Facebook hosting, make it live and it's fine from there.
     
  8. TheCodingGuy23

    TheCodingGuy23

    Joined:
    Mar 6, 2019
    Posts:
    9
    This is a really weird issue. The first time I tested it out, it worked fine. It only gave me issues the second time I ran the game. I will try logging the user in from the JavaScript SDK.
     
  9. TheCodingGuy23

    TheCodingGuy23

    Joined:
    Mar 6, 2019
    Posts:
    9
    I came across this a few days back on the Facebook developer page:
    I think this might be the problem. Will give it a try tomorrow to see if it works.
     
  10. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,186
    hmm...Weird, I've never seen this issue with our games on FB. We don't trigger it from an event on the web as the FB login is the only option. Curious about that, but then we're using the Unity SDK and not the JS SDK.
     
  11. TheCodingGuy23

    TheCodingGuy23

    Joined:
    Mar 6, 2019
    Posts:
    9
    Tried logging the user in through a click event using the Unity SDK, and I still get a popup error:
     
  12. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,186
    Code (CSharp):
    1.     public void FBLogin()
    2.     {
    3.         FB.Init(OnInitComplete, OnHideUnity);
    4.     }
    5.  
    6.     private void OnInitComplete()
    7.     {
    8.         if (FB.IsLoggedIn)
    9.         {
    10.             OnLoggedIn();
    11.         }
    12.         else
    13.         {
    14.             FB.LogInWithReadPermissions(new List<string>() { "public_profile", "email", "user_friends" }, LoginCallback);
    15.         }
    16.     }
    17.  
    18.     private void OnHideUnity(bool isGameShown)
    19.     {
    20.         Debug.Log("OnHideUnity");
    21.         if (!isGameShown)
    22.         {
    23.             // pause the game - we will need to hide
    24.             Time.timeScale = 0;
    25.         }
    26.         else
    27.         {
    28.             // start the game back up - we're getting focus again
    29.             Time.timeScale = 1;
    30.         }
    31.     }
    32.  
    33.     void LoginCallback(IResult result)
    34.     {
    35.         if (FB.IsLoggedIn)
    36.         {
    37.             OnLoggedIn();
    38.         }
    39.     }
    40.  
    So, the login code I use. The one difference is we do have the OnHideUnity part which sets timescale to 0. Not sure if this will help you, but our code works and we have not had any issues yet.
     
  13. TheCodingGuy23

    TheCodingGuy23

    Joined:
    Mar 6, 2019
    Posts:
    9
    Something important I did not mention was that I was using Facebook's Instant Games. I was told that Facebook.Unity is Facebook's main SDK for Facebook apps and will not work with Facebook's Instant Games. Unfortunately, there is no Instant Games SDK for Unity (except for some hacks and tricks). This was a direct response from one of the moderators on the Instant Games Community. This is why it was not working in my case. I will most probably need to transition to Web Games.
     
  14. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,186
  15. TheCodingGuy23

    TheCodingGuy23

    Joined:
    Mar 6, 2019
    Posts:
    9
    Yeah. I've seen some people recommend it. Will give it a try when I get back to work on Friday to see if it works.