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


    Dec 22, 2014
    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
    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


    Dec 22, 2014
    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

    My WebRequests class:
    Which was initially created in this video (and then updated with more options)

    First need to start a Device Login through
    Code (CSharp):
    2.             string url = "";
    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.             );
    17.     private class DeviceLogin {
    18.         public string access_token;
    19.         public string scope;
    20.     }
    Code (JavaScript):
    1. Success: {"code":"LONG_CODE","user_code":"USER_CODE","verification_uri":"https:\/\/\/device","expires_in":420,"interval":5}
    Then the user needs to log in through 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
    Code (CSharp):
    1.             string url = "";
    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.             );
    16.     private class DeviceLoginStatus {
    17.         public string access_token;
    18.         public string code;
    19.     }
    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();
    3.         await AuthenticationService.Instance.SignInWithFacebookAsync(token);
    4.         Debug.Log("SignIn is successful.");
    6.         Debug.Log("PlayerId: " + AuthenticationService.Instance.PlayerId);
    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.
    alysonlupo and jjbcariaso like this.
  3. jjbcariaso


    Oct 18, 2018
    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


    Jan 25, 2024
    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


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


    Oct 6, 2016
    Thanks it works!