Search Unity

problem with token sdk facebook

Discussion in 'Scripting' started by Bolt, Oct 31, 2014.

  1. Bolt

    Bolt

    Joined:
    Dec 15, 2012
    Posts:
    296
    This script with the sdk facebook makes sure to print me the name and cognme user logged in. How come every time I log in asking me the token? if i try with another profile will not work because the token is associated with my account.
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using System;
    5. using System.Collections.Generic;
    6.  
    7. public class init : ConsoleBase
    8. {
    9.     bool isInit = false;
    10.     string hasPerm = "***";
    11.     string fbname;
    12.     private void CallFBInit()
    13.     {
    14.         FB.Init(OnInitComplete, OnHideUnity);
    15.  
    16.     }
    17.     void OnGUI()
    18.     {
    19.         if (GUI.Button (new Rect (10, 10, 300, 100), "InitInit: " + isInit))
    20.             CallFBInit ();
    21.        
    22.         if (GUI.Button (new Rect (10, 110, 300, 100), "LogIn - Logged: " + FB.IsLoggedIn)) {
    23.                         CallFBLogin ();
    24.  
    25.  
    26.                 print ("NOME"+FB.UserId +"/first_name");
    27.            
    28.                 }
    29.         if (GUI.Button (new Rect (10, 210, 300, 100), "Permission - friends: " + hasPerm)) {
    30.                         CallPermissionCheck ();
    31.  
    32.  
    33.                 }
    34.  
    35.         GUI.Label(new Rect(10, 10, 200, 200), fbname);
    36.  
    37.  
    38.     }
    39.    
    40.  
    41.    
    42.     private void OnInitComplete()
    43.     {
    44.         Debug.Log("FB.Init completed: Is user logged in? " + FB.IsLoggedIn);
    45.         isInit = true;
    46.     }
    47.    
    48.     private void OnHideUnity(bool isGameShown)
    49.     {
    50.         Debug.Log("Is game showing? " + isGameShown);
    51.     }
    52.    
    53.     private void CallFBLogin()
    54.     {
    55.         FB.Login("publish_actions, user_friends", LoginCallback);
    56.  
    57.     }
    58.    
    59.     void LoginCallback(FBResult result)
    60.     {
    61.         string lastResponse;
    62.        
    63.         if (result.Error != null)
    64.             lastResponse = "Error Response:\n" + result.Error;
    65.         else if (!FB.IsLoggedIn)
    66.         {
    67.             lastResponse = "Login cancelled by Player";
    68.         }
    69.         else
    70.         {
    71.             lastResponse = "Login was successful!";
    72.         }
    73.        
    74.         Debug.Log("lastResponse: "+lastResponse);
    75.     }
    76.    
    77.     void CallPermissionCheck()
    78.     {
    79.         FB.API("/me/permissions", Facebook.HttpMethod.GET, PermissionCallback);
    80.  
    81.     }
    82.    
    83.     void PermissionCallback(FBResult result)
    84.     {
    85.         if (!String.IsNullOrEmpty(result.Error))
    86.         {
    87.             Debug.Log("Error Response:\n" + result.Error);
    88.         }
    89.         else
    90.         {
    91.             Debug.Log("Get user's permissions was successful!");
    92.             Debug.Log ("Result: "+result.Text);
    93.            
    94.            
    95.             var permissionDict = Facebook.MiniJSON.Json.Deserialize(result.Text) as Dictionary<string, object>;
    96.             IDictionary persmissionData = permissionDict;
    97.             foreach(IDictionary perm in (List<object>)persmissionData["data"])
    98.             {
    99.                 if((string)perm["permission"] == "user_friends")
    100.                 {
    101.                     hasPerm = (string)perm["status"];
    102.                    
    103.                     // This will deauthorise the permissions for the user and then log them out
    104.                     /*
    105.                      if(hasPerm != "granted")
    106.                      {
    107.                          FB.API ("me/permissions", Facebook.HttpMethod.DELETE, RemovePermissionCallback);
    108.                          FB.Logout();
    109.                      }
    110.                      */
    111.                     FB.API("me?fields=name", Facebook.HttpMethod.GET, NameCallBack);
    112.  
    113.                 }
    114.             }
    115.            
    116.         }
    117.     }
    118.    
    119.     void RemovePermissionCallback(FBResult result)
    120.     {
    121.         if (!String.IsNullOrEmpty(result.Error))
    122.         {
    123.             Debug.Log("Error Response:\n" + result.Error);
    124.         }
    125.         else
    126.         {
    127.             Debug.Log("RemovePermissionCallback was successful!");
    128.             Debug.Log ("Result: "+result.Text);
    129.         }
    130.     }
    131.  
    132.     void NameCallBack(FBResult result)
    133.     {
    134.         IDictionary dict = Facebook.MiniJSON.Json.Deserialize(result.Text) as IDictionary;
    135.         fbname = dict["name"].ToString();
    136.  
    137.  
    138.     }
    139.    
    140. }
    141.