Cannot Authenticate with Facebook

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 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.

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 For Devices - Facebook Login - Documentation - Meta for Developers

My WebRequests class: https://unitycodemonkey.com/misc/WebRequests.cs
Which was initially created in this video (and then updated with more options)
https://www.youtube.com/watch?v=Gk0-amfn5DM

First need to start a Device Login through https://graph.facebook.com/v2.6/device/login

            string url = "https://graph.facebook.com/v2.6/device/login";
            string facebookAppId = "APP_ID";
            string facebookClientToken = "CLIENT_ID_TOKEN";
            DeviceLogin deviceLogin = new DeviceLogin {
                access_token = $"{facebookAppId}|{facebookClientToken}",
                scope = "public_profile",
            };
            WebRequests.PostJson(url, JsonUtility.ToJson(deviceLogin)
                , (string err) => {
                    Debug.Log("Error: " + err);
                }, (string success) => {
                    Debug.Log("Success: " + success);
                }
            );

    private class DeviceLogin {
        public string access_token;
        public string scope;
    }
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 Redirecting... 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

            string url = "https://graph.facebook.com/v2.6/device/login_status";
            string facebookAppId = "APP_ID";
            string facebookClientToken = "CLIENT_TOKEN";
            DeviceLoginStatus deviceLoginStatus = new DeviceLoginStatus {
                access_token = $"{facebookAppId}|{facebookClientToken}",
                code = "LONG_CODE",
            };
            WebRequests.PostJson(url, JsonUtility.ToJson(deviceLoginStatus)
                , (string err) => {
                    Debug.Log("Error: " + err);
                }, (string success) => {
                    Debug.Log("Success: " + success);
                }
            );

    private class DeviceLoginStatus {
        public string access_token;
        public string code;
    }
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

        await UnityServices.InitializeAsync();

        await AuthenticationService.Instance.SignInWithFacebookAsync(token);
        Debug.Log("SignIn is successful.");

        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.
Thanks!

2 Likes

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

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”

4 Likes

You’re a saviour. Thank you very much. Why this information is nowhere to be found?

Thanks it works!