0

My Url is like below...

http://localhost:1561/Facebook/Facebook/MainPage

Controller Class

public class FacebookController : Controller
{
    public ActionResult MainPage()
    {
        return View();
    }

    public PartialViewResult ShowMyDetails()
    {
        return PartialView("FacebookUserControl");
    }

    [CanvasAuthorize(Permissions = "user_about_me, publish_stream")]
    public ActionResult GetMyDetails()
    {
        var fb = new FacebookClient();
        dynamic CurrentUser = fb.Get("100003533388420");
        User user = new User
        {
            id = CurrentUser.id,
            first_name = CurrentUser.first_name,
            gender = CurrentUser.gender,
            last_name = CurrentUser.last_name,
            name = CurrentUser.name,
            username = CurrentUser.username
        };
        return Json
        (
            user,
            JsonRequestBehavior.AllowGet
        );
    }
}

I don't have any Facebook related in my Web.Config.

Issue - In case I add below mentioned line. I am facing Issue "settings.AppId is null"

[CanvasAuthorize(Permissions = "user_about_me, publish_stream")]
4

2 回答 2

0

Let me preface this by saying that I have not worked on a canvas app before, so not really sure what the differences are.

But, when creating the FacebookClient, you need to provide the accessToken.

        public ActionResult LoginFacebookAuth(string access_token)
        {
            var accessToken = access_token;

            try
            {
                var facebookClient = new Facebook.FacebookClient(accessToken);

                dynamic me = facebookClient.Get("me");
                var fbUserId = me.id.ToString();
                var email = me.email.ToString();

...

Also, in web.config, I do have an entry.

<appSettings>
   <add key="FbAppId" value="" />

Code in View that passes in access_token:

window.fbAsyncInit = function() {
                FB.init({
                    appId: '{FbAppId from web.config}',
                    status: true, // check login status
                    cookie: true, // enable cookies to allow the server to access the session
                    xfbml: true  // parse XFBML
                });
            };
            $(document).ready(function() {
                $('body').append('<div id="fb-root" name="fb-root"></div>');
                $('.fbLoginLink') //When my fb icon is clicked, call FbLogin
                    .click(function(e) {
                        e.preventDefault();
                        FbLogin();
                    });


            });
            // 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));

            function FbLogin() {
                FB.login(
                    function(response) {
                        if (response.authResponse) {
                            var uid = response.authResponse.userID;
                            var accessToken = response.authResponse.accessToken;
                            window.location = '@Url.RouteUrl("LoginFacebookAuth", new { })?accessToken=' + accessToken + '&uid=' + uid;
                        } else {
                            window.location = '/'; //User cancelled authorization
                        }
                    },
                    { scope: 'email,user_birthday' }
                ); //FB.login
            }
于 2013-03-04T17:53:36.173 回答
0
public ActionResult GetFriendsDetails()
{
    List<User> UserList = new List<Models.User>();
    var fb = new FacebookClient("MyAppID", "MySecret");
    //This will give the Access Token
    dynamic FriendsList = fb.Get("MyAppID/friends");
    foreach (var item in FriendsList.data)
    {
        dynamic CurrentUser = fb.Get(item.id);
        UserList.Add
        (
            new User
            {
                id = CurrentUser.id,
                first_name = CurrentUser.first_name,
                gender = CurrentUser.gender,
                last_name = CurrentUser.last_name,
                name = CurrentUser.name,
                username = CurrentUser.username
            }
        );
    }
    return Json
    (
        UserList,
        JsonRequestBehavior.AllowGet
    );
}
于 2013-03-05T06:43:50.513 回答