Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only. On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live. Read our full announcement for more information and let us know if you have any questions.

Bug Cannot Authenticate with Facebook

Discussion in 'Authentication' started by CodeMonkeyYT, May 6, 2023.

  1. CodeMonkeyYT

    CodeMonkeyYT

    Joined:
    Dec 22, 2014
    Posts:
    125
    Hey everyone,

    I've spent the past 6 hours trying to implement Facebook log in and nothing works.

    I followed the steps on the Docs https://docs.unity.com/authentication/en/manual/platform-signin-facebook
    Downloaded the Facebook Unity SDK, created a Facebook App of type "Facebook Login" and enabled "Facebook Gaming Login" and enabled "Log in from Device"
    With that I was able to log in using the WindowsExample demo scene included in the Facebook Unity SDK.
    However then I could not log in with the Unity Authentication through AuthenticationService.Instance.SignInWithFacebookAsync(token); the result was "Request failed: 401, {"title":"PERMISSION_DENIED","detail":"unable to validate token","details":[],"status":401}"

    After searching I found out that apparently Unity Auth only works with Facebook App type "Consumer" and not "Facebook Login", so then I created another Facebook App, this time I made the type "Consumer" except now I cannot log in using the WindowsExample demo scene, I get the error "Error validating application. Invalid application ID."
    Trying to log in with Unity Auth simply returns the same error message and then "[Facebook Login] User cancelled login"

    I even tried requesting Advanced Access to "public_profile" which required me to create and validate a Business Account, but even after that nothing works.

    I'm really at a loss, no idea what I'm doing wrong.
    The only thing I can think of is the Facebook Unity SDK only works with "Facebook Login" and not with "Consumer", but if that's the case how come the Docs say to use that method?
    I'm going to try doing an HTTP request myself instead of using the SDK.
     
  2. CodeMonkeyYT

    CodeMonkeyYT

    Joined:
    Dec 22, 2014
    Posts:
    125
    Finally got it working by doing the HTTP request myself instead of using the Facebook Unity SDK, that still seems like a bug because the Docs explicitly say to use that SDK which apparently does not work with "Consumer" Facebook Apps.

    Here's what I needed to do in case someone comes across this issue. Basically I had to do the OAuth process manually, I used their Device Login method https://developers.facebook.com/docs/facebook-login/for-devices/

    My WebRequests class: https://unitycodemonkey.com/misc/WebRequests.cs
    Which was initially created in this video (and then updated with more options)


    First need to start a Device Login through https://graph.facebook.com/v2.6/device/login
    Code (CSharp):
    1.  
    2.             string url = "https://graph.facebook.com/v2.6/device/login";
    3.             string facebookAppId = "APP_ID";
    4.             string facebookClientToken = "CLIENT_ID_TOKEN";
    5.             DeviceLogin deviceLogin = new DeviceLogin {
    6.                 access_token = $"{facebookAppId}|{facebookClientToken}",
    7.                 scope = "public_profile",
    8.             };
    9.             WebRequests.PostJson(url, JsonUtility.ToJson(deviceLogin)
    10.                 , (string err) => {
    11.                     Debug.Log("Error: " + err);
    12.                 }, (string success) => {
    13.                     Debug.Log("Success: " + success);
    14.                 }
    15.             );
    16.  
    17.     private class DeviceLogin {
    18.         public string access_token;
    19.         public string scope;
    20.     }
    21.  
    Code (JavaScript):
    1. Success: {"code":"LONG_CODE","user_code":"USER_CODE","verification_uri":"https:\/\/www.facebook.com\/device","expires_in":420,"interval":5}
    Then the user needs to log in through https://www.facebook.com/device using the Code generated from the previous API call. Note: it's the short user_code with just about 5 letters, not the big code with about 20 characters.

    Then need to poll Device Login Status through https://graph.facebook.com/v2.6/device/login_status
    Code (CSharp):
    1.             string url = "https://graph.facebook.com/v2.6/device/login_status";
    2.             string facebookAppId = "APP_ID";
    3.             string facebookClientToken = "CLIENT_TOKEN";
    4.             DeviceLoginStatus deviceLoginStatus = new DeviceLoginStatus {
    5.                 access_token = $"{facebookAppId}|{facebookClientToken}",
    6.                 code = "LONG_CODE",
    7.             };
    8.             WebRequests.PostJson(url, JsonUtility.ToJson(deviceLoginStatus)
    9.                 , (string err) => {
    10.                     Debug.Log("Error: " + err);
    11.                 }, (string success) => {
    12.                     Debug.Log("Success: " + success);
    13.                 }
    14.             );
    15.  
    16.     private class DeviceLoginStatus {
    17.         public string access_token;
    18.         public string code;
    19.     }
    20.  
    Code (JavaScript):
    1. Success: {"access_token":"FACEBOOK_ACCESS_TOKEN","data_access_expiration_time":1691168246,"expires_in":5183589}
    Finally I can log in through Unity Auth with that access token to get a Unity PlayerId
    Code (CSharp):
    1.         await UnityServices.InitializeAsync();
    2.  
    3.         await AuthenticationService.Instance.SignInWithFacebookAsync(token);
    4.         Debug.Log("SignIn is successful.");
    5.  
    6.         Debug.Log("PlayerId: " + AuthenticationService.Instance.PlayerId);
    7.  
    I really hope this post helps someone, and I hope either the Docs are updated or someone tell me what I was doing wrong with the official Facebook Unity SDK.
    Thanks!
     
    alysonlupo and jjbcariaso like this.
  3. jjbcariaso

    jjbcariaso

    Joined:
    Oct 18, 2018
    Posts:
    3
    Hi, I copied the device login code, but it give me Error: HTTP/1.1 400 Bad Request. Both my facebookAppId and facebookClientToken is same with the one in my facebook dashboard.

    thanks in advance
     
  4. DaniyalRizviNW

    DaniyalRizviNW

    Joined:
    Jan 25, 2024
    Posts:
    1
    Just add GG| as a prefix before your Id in the unity editor in Project settings -> Services -> Authentication. Your new Id will look something like "GG|703975117224545" instead of "703975117224545"
     
    bhunterd and pocketapex like this.
  5. pocketapex

    pocketapex

    Joined:
    Sep 25, 2023
    Posts:
    3
    You're a saviour. Thank you very much. Why this information is nowhere to be found?
     
  6. bhunterd

    bhunterd

    Joined:
    Oct 6, 2016
    Posts:
    43
    Thanks it works!