Accessing facebook for posts etc with Graph API c# unity3d

前端 未结 2 1907
清歌不尽
清歌不尽 2021-01-01 06:44

I\'m using C# in MonoDevelop with the Graph API to interact with users facebook pages.

I\'ve become thoroughly confused though.

Are these the correct steps?<

相关标签:
2条回答
  • 2021-01-01 07:12

    Use the C# Facebook library. There is a sample project called facebook-aspnet-sample which shows you authentication and login. Also, have a look at this tutorial page which deals with how to connect from a Unity3d game to Facebook. The tut is short but the demo project download is useful.

    There are also plugins for sale that enable Facebook access from Unity3d.

    0 讨论(0)
  • 2021-01-01 07:25

    after your have your FB app ready, indeed, you need to get a token. However, token is always requested in the context of a USER - and therefore user should authenticated on the website. That means that Facebook login dialog should be displayed - which will ask user if he indeed trusts the application. There are different ways to do this - here is the generic page about FB authentication: https://developers.facebook.com/docs/authentication/, and this one explains how to do this in Android: https://developers.facebook.com/docs/mobile/android/sso/

    if you want to go with JavaScript authentication (which I personally use in MVC3 project), here is a little bit modified example from https://developers.facebook.com/docs/authentication/client-side/ which does this. Remember to change app id and permissions to the ones you need.

    <div id="fb-root"></div>
    <script>
        // Load the SDK Asynchronously
        (function (d) {
            var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
            if (d.getElementById(id)) { return; }
            js = d.createElement('script'); js.id = id; js.async = true;
            js.src = "//connect.facebook.net/en_US/all.js";
            ref.parentNode.insertBefore(js, ref);
        } (document));
    
        // Init the SDK upon load
        window.fbAsyncInit = function () {
            FB.init({
                appId: '@EventController.FB_APP_ID', // App ID
                channelUrl: '//' + window.location.hostname + '/channel.htm', // Path to your Channel File
                status: false, // check login status
                cookie: false, // enable cookies to allow the server to access the session
                xfbml: false  // parse XFBML
            });
    
            // listen for and handle auth.statusChange events
            FB.Event.subscribe('auth.statusChange', function (response) {
                if (response.authResponse) {
                    // user has auth'd your app and is logged into Facebook
                    FB.api('/me', function (me) {
                        // get info about the user, for example
                    })
                    //document.getElementById('auth-loggedout').style.display = 'none';
                } else {
                    // user has not auth'd your app, or is not logged into Facebook
                    //document.getElementById('auth-loggedout').style.display = 'block';
                }
            });
    
            // respond to clicks on the login and logout links
            document.getElementById('auth-loginlink').addEventListener('click', function () {
                FB.login(function (response) {
                    // handle the response
                    if (response.authResponse != null) {
                        // HERE IS THE TOKEN YOU CAN USE
                        var token = response.authResponse.accessToken;
                    } else {
    
                    }
                    // AND THESE ARE THE PERMISSIONS YOU WANT YOUR APP TO REQUEST
                }, { scope: 'manage_pages,publish_stream' });
            });
        };
    </script>
    

    One catch here - your FB app should have your website URL (even if it is http://localhost/) in App -> Settings -> Website URL field. Also you should put channel.htm file into the root of your website with one line of code in it:

    <script src="//connect.facebook.net/en_US/all.js">
    
    0 讨论(0)
提交回复
热议问题