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

Can someone help me with GameSparks FB login

Discussion in 'Scripting' started by cyryllgalon, Feb 23, 2016.

  1. cyryllgalon

    cyryllgalon

    Joined:
    Feb 23, 2016
    Posts:
    1
    so im having an error Assets/FacebookSDK/SDK/Scripts/LoginManager.cs(25,144): error CS0136: A local variable named `obj' cannot be declared in this scope because it would give a different meaning to `obj', which is already used in a `parent or current' scope to denote something else

    i really dont know how to fix this error because some namespace are change because of the updates, that's why i can't follow the scripts in the video properly...


    this is the tutorial i've been following https://docs.gamesparks.net/tutorials/unity-tutorial-logging-in-to-gamesparks-with-facebook

    my script is in the attached files.
     

    Attached Files:

    • dsa.png
      dsa.png
      File size:
      115.8 KB
      Views:
      1,077
  2. Patrick-GameSparks

    Patrick-GameSparks

    Joined:
    Feb 2, 2016
    Posts:
    23
    Hi cyryllgalon,
    I'm part of the customer support team at www.gamesparks.com . A Facebook SDK update caused some changes to the login process. Specifically when integrating Facebook into GameSparks. Our tutorials are in the process of being updated but I can pass you an updated version of the class you are using to handle your GameSparks and Facebook Login.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using GameSparks.Core;
    4. using System.Collections.Generic;
    5. using System;
    6. using GameSparks.Api.Responses;
    7. using Facebook.Unity;
    8.  
    9. public class GameSparksManager : MonoBehaviour {
    10.  
    11.  
    12.     //singleton for the gamesparks manager so it can be called from anywhere
    13.     private static GameSparksManager instance = null;
    14.     //getter property for private backing field instance
    15.     public static GameSparksManager Instance() { return instance; }
    16.  
    17.     // Use this for initialization
    18.     void Awake ()
    19.     {
    20.         //this will create a singleton for our gamesparks manager object
    21.         if(instance = null)
    22.         {
    23.             instance = this;
    24.             DontDestroyOnLoad(this.gameObject);
    25.         }
    26.         else
    27.         {
    28.             DontDestroyOnLoad(this.gameObject);
    29.         }
    30.         GS.GameSparksAvailable += GSAvailable;
    31.         GameSparks.Api.Messages.AchievementEarnedMessage.Listener += AchievementEarnedListener;
    32.     }
    33.    
    34.     void GSAvailable (bool _isAvalable)
    35.     {
    36.         //this method will be called only when the GS service is available or unavailable
    37.         if(_isAvalable)
    38.         {
    39.            // Application.LoadLevel(1);
    40.             Debug.Log(">>>>>>>>>GS Conected<<<<<<<<");
    41.         }
    42.         else
    43.         {
    44.             Debug.Log(">>>>>>>>>GS Disconnected<<<<<<<<");
    45.         }
    46.     }
    47.    
    48.     //Achievement message  listener
    49.     private void AchievementEarnedListener (GameSparks.Api.Messages.AchievementEarnedMessage _message)
    50.     {
    51.         Debug.LogWarning("Message Recieved" + _message.AchievementName);
    52.  
    53.     }
    54.  
    55.    
    56.  
    57.     #region FaceBook Authentication
    58.     /// <summary>
    59.     /// Below we will login with facebook.
    60.     /// When FB is ready we will call the method that allows GS to connect to GameSparks
    61.     /// </summary>
    62.     public void ConnectWithFacebook()
    63.     {
    64.         if(!FB.IsInitialized)
    65.         {
    66.             Debug.Log("Initializing Facebook");
    67.             FB.Init(FacebookLogin);
    68.         }
    69.         else
    70.         {
    71.             FacebookLogin();
    72.         }
    73.     }
    74.    
    75.  
    76.     /// <summary>
    77.     /// When Facebook is ready , this will connect the pleyer to Facebook
    78.     /// After the Player is authenticated it will  call the GS connect
    79.     /// </summary>
    80.     void FacebookLogin()
    81.     {
    82.         if(!FB.IsLoggedIn)
    83.         {
    84.             Debug.Log("Logging into Facebook");
    85.             FB.LogInWithReadPermissions(
    86.                 new List<string>() { "public_profile", "email", "user_friends" },
    87.                 GameSparksFBConnect
    88.                 );
    89.         }
    90.     }
    91.    
    92.     void GameSparksFBConnect(ILoginResult result)
    93.     {
    94.      
    95.         if(FB.IsLoggedIn)
    96.         {
    97.             Debug.Log("Logging into gamesparks with facebook details");
    98.             GSFacebookLogin(AfterFBLogin);
    99.         }
    100.         else
    101.         {
    102.             Debug.Log("Something wrong  with FB");
    103.         }
    104.     }
    105.  
    106.     //this is the callback that happens when gamesparks has been connected with FB
    107.     private void AfterFBLogin(GameSparks.Api.Responses.AuthenticationResponse _resp)
    108.     {
    109.         Debug.Log(_resp.DisplayName );
    110.     }
    111.  
    112.     //delegate for asynchronous callbacks
    113.     public delegate void FacebookLoginCallback(AuthenticationResponse _resp);
    114.    
    115.  
    116.     //This method will connect GS with FB
    117.     public void GSFacebookLogin(FacebookLoginCallback _fbLoginCallback )
    118.     {
    119.         Debug.Log("");
    120.  
    121.         new GameSparks.Api.Requests.FacebookConnectRequest()
    122.             .SetAccessToken(AccessToken.CurrentAccessToken.TokenString)
    123.             .Send((response) => {
    124.                 if(!response.HasErrors)
    125.                 {
    126.                     Debug.Log("Logged into gamesparks with facebook");
    127.                     _fbLoginCallback(response);
    128.                 }
    129.                 else
    130.                 {
    131.                     Debug.Log("Error Logging into facebook");
    132.                 }
    133.              
    134.             });
    135.     }
    136.     #endregion
    137.  
    138.     /// <summary>
    139.     /// If a player is registered this will log them in with GameSparks.
    140.     /// </summary>
    141.     public void LoginPlayer(string _userNameInput, string _passwordInput)
    142.     {
    143.         new GameSparks.Api.Requests.AuthenticationRequest()
    144.             .SetUserName(_userNameInput)
    145.             .SetPassword(_passwordInput)
    146.             .Send((response) => {
    147.                 if (!response.HasErrors)
    148.                 {
    149.                     Debug.Log("Player Authenticated...");
    150.                 }
    151.                 else
    152.                 {
    153.                     Debug.Log("Error Authenticating Player\n" + response.Errors.JSON.ToString());
    154.                 }
    155.             });
    156.     }
    157.  
    158.     /// <summary>
    159.     /// this will register a new player and assign their email to their account.
    160.     /// </summary>
    161.     public void RegisterNewPlayer(string _userNameInput, string _emailInput, string _passwordInput)
    162.     {
    163.         new GameSparks.Api.Requests.RegistrationRequest()
    164.             .SetDisplayName(_userNameInput)
    165.             .SetUserName(_userNameInput)
    166.             .SetPassword(_passwordInput)
    167.             .SetScriptData(new GSRequestData().AddString("email", _emailInput))
    168.             .Send((response) =>
    169.             {
    170.                 if (!response.HasErrors)
    171.                 {
    172.                     Debug.Log("Player registered");
    173.                 }
    174.                 else
    175.                 {
    176.                     Debug.LogWarning("Failed to register player...\n" + response.Errors.JSON.ToString());
    177.                 }
    178.  
    179.  
    180.             });
    181.     }
    182.  
    183.  
    184. }
    185.  
    This should resolve you issue. If you ahve any further issues. Please don't hesitate to make use of our active community forums located here: https://support.gamesparks.net/discussions/forums/1000077208
    Or our ticket system where our dedicated support team will be able to assist you directly: https://support.gamesparks.net/helpdesk/tickets
    Hope I have been of help. Looking forward to hearing from you.
    Best Regards, Patrick-GameSparks.
     
  3. the88g

    the88g

    Joined:
    May 14, 2015
    Posts:
    1
    This updated script works well for Facebook login after the SDK changes. Will you be updating your website documentation soon? It took me some time to stumble upon this.

    Am I right in saying that this doesn't work for users coming back wanting to sign in again with their already authorised facebook account?

    I find that when I assign a button to ConnectWithFacebook(), it only works the very first time. I've added the following section under FacebookLogin() and it seems to have fixed the problem but could someone double check it as I want to make sure the authentication is working correctly for returning users:

    Code (CSharp):
    1.         if (FB.IsLoggedIn) {
    2.             GSFacebookLogin (AfterFBLogin);
    3.         }
    FacebookLogin() now looks like this;

    Code (CSharp):
    1.     void FacebookLogin ()
    2.     {
    3.         if (!FB.IsLoggedIn) {
    4.             Debug.Log ("Logging into Facebook");
    5.             FB.LogInWithReadPermissions (
    6.                 new List<string> () { "public_profile", "email", "user_friends" },
    7.                 GameSparksFBConnect
    8.             );
    9.         }
    10.  
    11.         if (FB.IsLoggedIn) {
    12.             GSFacebookLogin (AfterFBLogin);
    13.         }
    14.     }
     
  4. Luke-Houlihan

    Luke-Houlihan

    Joined:
    Jun 26, 2007
    Posts:
    303
    You have to save the facebook access token on the initial log in, then on return just feed it into the GameSparks FacebookConnectionRequest. See my code below for an example.

    Theres probably a better way to do it but this has been working for me.

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using System.Linq;
    6.  
    7. using Facebook;
    8. using Facebook.Unity;
    9. using GameSparks.Api;
    10. using GameSparks.Api.Requests;
    11. using GameSparks.Api.Responses;
    12.  
    13. public class Auth : MonoBehaviour {
    14.  
    15.     //General player information variables
    16.     public string fbAuthToken = "";
    17.  
    18.     void Awake(){
    19.         //load authtoken from playerprefs and see if it's been filled in
    20.         Load ();
    21.         if (fbAuthToken != "") {
    22.             GameSparksFBLogBackIn ();
    23.         }
    24.     }
    25.  
    26.     public void CallFBInit(){
    27.         //initialize facebook
    28.         FB.Init(this.OnInitComplete, this.OnHideUnity);
    29.     }
    30.  
    31.     private void OnInitComplete(){
    32.         Debug.Log ("FB.Init completed: Is user logged in? " + FB.IsLoggedIn);
    33.         CallFBLogin ();
    34.     }
    35.  
    36.     private void CallFBLogin(){
    37.         FB.LogInWithReadPermissions (new List<string>() { "public_profile", "email"}, GameSparksFBLogin);
    38.     }
    39.  
    40.     private void GameSparksFBLogin(ILoginResult result){
    41.         if (FB.IsLoggedIn) {
    42.             fbAuthToken = AccessToken.CurrentAccessToken.TokenString;
    43.             Save ();
    44.             new FacebookConnectRequest ()
    45.                 .SetAccessToken (AccessToken.CurrentAccessToken.TokenString)
    46.                 .SetSwitchIfPossible (true)
    47.                 .SetSyncDisplayName(true)
    48.                 .Send ((response) => {
    49.                 if (response.HasErrors) {
    50.                     Debug.Log ("Something failed when connecting with Facebook - "+result.Error);
    51.                 } else {
    52.                     Debug.Log ("Gamesparks Facebook login successful");
    53.                 }
    54.             });
    55.         }
    56.     }
    57.  
    58.     private void GameSparksFBLogBackIn(){
    59.         new FacebookConnectRequest ()
    60.             .SetAccessToken (fbAuthToken)
    61.             .Send ((response) => {
    62.             if (response.HasErrors) {
    63.                 Debug.Log ("Something failed when connecting with Facebook on log back in");
    64.             } else {
    65.                 Debug.Log ("Gamesparks Facebook login successful on log back in");
    66.             }
    67.         });
    68.     }
    69.  
    70.     private void OnHideUnity(bool isGameShown){
    71.  
    72.     }
    73.  
    74.     void Save()
    75.     {
    76.         PlayerPrefs.SetString("fbAuthToken", fbAuthToken);
    77.     }
    78.  
    79.     void Load()
    80.     {
    81.         fbAuthToken = PlayerPrefs.GetString("fbAuthToken");
    82.     }
    83. }
    84.  
     
  5. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,186
    I'm not using GameSparks (using other similar services), but with Facebook, don't you run into issues with expired access tokens? From the looks of it, your LogBackIn call never logs you back into Facebook? Unless I misread, which means you wouldn't have any of the FB social features if you were using those.
     
  6. Luke-Houlihan

    Luke-Houlihan

    Joined:
    Jun 26, 2007
    Posts:
    303
    @Brathnann I only posted the non-project specific areas of my Auth class, when the LogBackIn method fails I have a UI popup asking if the user would like to sign back in.
    As far as I understand it the access token is refreshed every time it is used, if it is not used, it expires in 60 days, or when nullified by dis-allowing the app access to the users facebook account.

    Here is the relevant info on facebooks docs-
    https://developers.facebook.com/docs/facebook-login/access-tokens/expiration-and-extension
     
  7. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,186
    Yep, I have read the docs, already have a game live on FB, was just curious how you were handling possible expired tokens. We just init Facebook when a person loads up the game, which gives us the token. Of course, with webGL on Facebook, we can't use playerprefs, so even our mobile version just follows the same pattern.
     
  8. Luke-Houlihan

    Luke-Houlihan

    Joined:
    Jun 26, 2007
    Posts:
    303
    Doesn't calling the init function just pull up the authorization screen again? When I tried doing it this way, I got a facebook popup saying "This app is already authorized" with the option of "ok". I didn't want the user to have to see that every time the app is launched. Are you doing it in the background somehow?
     
  9. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,186
    On mobile, this only occurs if the user does a fresh install. In which case, if they don't have the Facebook app, they are prompted to log in, after which it will say they already authorized. (This will only happen once). I think Facebook might be storing something in the cache, as I think you can clear the cache and it will prompt you again if I remember correctly.

    If they have the Facebook app and are logged into it, it seems to get the info from there, so they don't have to log in. But if a player logs out of the Facebook app, I think it prompts again. I know we did a lot of test on this before.

    On Facebook itself, it just logs them straight in. But they will get prompts if they removed the app from their authorized apps, which removes any requested permissions.

    My understanding of FB.init was without it you couldn't do any of the other functions and Facebook wouldn't record that a person was playing your game. So we just always run it at the start before doing the loginWithReadPermissions.

    I am curious if you were getting that notice every time you loaded up the game what the reason for that was, as I get different results.
     
  10. Liam_GameSparks

    Liam_GameSparks

    Joined:
    Sep 10, 2015
    Posts:
    1
    Hi Guys,

    If you have any further queries on this in relation to connect Facebook to GameSparks please head on over to our support page located here.

    Thanks,
    Liam