Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • mohammed 17 posts 88 karma points
    Mar 12, 2015 @ 19:26
    mohammed
    0

    Call action from ajax to get user details from Facebook

    Hi, 

    I am using Facebook on my app to get a new users details. I created an app on Facebook and got the user access token as well. I now need to do an ajax call to a method on the login controller and get the users details. I am geting error:  POST http://localhost:43568/AccountSurface/FacebookCallback 404 (Not Found)

    Below is my script and my action in the Account Controller. Please have a look and tell me what I am doing wrong:

     window.fbAsyncInit = function () {

                                               FB.init({

                                                   appId: '1XXXXX', //Facebook App ID

                                                   status: true, // check login status

                                                   cookie: true, // enable cookies to allow the server to access the session

                                                   xfbml: true  // parse XFBML

                                               });

     

                                               // Additional initialization code here

                                               FB.Event.subscribe('auth.authResponseChange', function (response) {

                                                   if (response.status === 'connected') {

     

                                                       var uid = response.authResponse.userID;

                                                       var accessToken = response.authResponse.accessToken;

     

     

                                                       $.ajax({

                                                           url: '/AccountSurface/FacebookCallback',

                                                           dataType: "json",

                                                           type: "POST",

                                                           contentType: 'application/json; charset=utf-8',

                                                           data: JSON.stringify({ accessToken: accessToken }),

                                                           async: true,

                                                           processData: false,

                                                           cache: false,

                                                           success: function (data) {

                                                               alert(data);

                                                           },

                                                           error: function (xhr) {

                                                               alert('error');

                                                           }

                                                       })

                                                   } else if (response.status === 'not_authorized') {

                                                       // the user is logged in to Facebook, 

                                                       // but has not authenticated your app

                                                   } else {

                                                       // the user isn't logged in to Facebook.

                                                   }

                                               });

                                           };

     

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

     

     

    Facebook Call Action:

      [NotChildAction]

            [HttpPost]

            public ActionResult FacebookCallback(string code)

            {

                FacebookClient fb = new FacebookClient();

                var result2 = fb.Get("me?fields=first_name,middle_name,last_name,id,email");

                dynamic result = fb.Post("oauth/access_token", new

                {

                    client_id = "XXXXXX",

                    client_secret = "xxxxxxxx",

                    redirect_uri = "http%3a%2f%2flocalhost%3a43568%2fRegister",

                    code = code

                });    

                       return RedirectToAction("Index", "Home");

            }

     

    Thanks for all the help

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies