August 11, 2013
In my old post I shown you how to authorize a user via Facebook and access the Facebook API in your desktop Unity game. Unfortunately this involved the user to copy and paste an access token from their browser in to your game. Well now I’ve revisited this and made it fully automated with a nice smooth process.
Here’s how it works:
Application.OpenURL())) to a Facebook login prompt, passing along a uniquely generated id.
And here’s a video of it in action:
I’m not going to create a tutorial on how to achieve this as I believe code speakers louder than words. In the project below you’ll find a sample Unity scene, a modified version of the Facebook C# SDK and a basic ASP.NET MVC website to handle the token exchange. Check it out, use it how you wish and if you have any questions drop me a comment below.
To handle the token exchange between your Unity game and the users browser you will need a web service. I have included the source code for my example in the zip above and would recommend running your own service. However, if you’re just experimenting with Unity and want to get something up and running quickly or creating a small game between friends and still want to offer Facebook integration I’ve created a hosted service which is very easy to use:
_redirectEndpointproperty to point to
&scope=query string in the Facebook redirect URL (on the
And that’s it! You’ll get returned an access token which will be valid for 60 days from the time of issue, use this token to make requests against the Facebook API as shown in my example. Once the token has expired make sure to push your user through the same flow to get a new token. If you have any suggestions please let me know.
© 2014 Paul's Blog
Comments or opinions expressed on the blog are those of their respective contributors only. I am not responsible for, and disclaims any and all liability for the content of comments written by contributors to the blog.