English 中文(简体)
• 如何从javascript SDK的FB.login方法中获取
原标题:How to get access token from FB.login method in javascript SDK
  • 时间:2011-01-21 12:26:58
  •  标签:
  • facebook

我需要从Javascript SDK的方法中获取。 我的标识法

FB.login(function(response) {
    if (response.session) {
        if (response.perms) {

        } else {
            // user is logged in, but did not grant any permissions
            alert("No Permission..");
        }
    } else {
        // user is not logged in
        alert("Please login to facebook");
    }
}, {perms: read_stream,publish_stream,offline_access });

Is there any way to get access token? I am able to get the access token using PHP.

最佳回答

您可使用<条码>F.B.getAuthResponse(出入<<>>代码>):

FB.login(function(response) {
   if (response.authResponse) {
     var access_token =   FB.getAuthResponse()[ accessToken ];
     console.log( Access Token =  + access_token);
     FB.api( /me , function(response) {
     console.log( Good to see you,   + response.name +  . );
     });
   } else {
     console.log( User cancelled login or did not fully authorize. );
   }
 }, {scope:   });

Edit: Updated to use Oauth 2.0, required since December 2011. Now uses FB.getAuthResponse(); If you are using a browser that does not have a console, (I m talking to you, Internet Explorer) be sure to comment out the console.log lines or use a log-failsafe script such as:

if (typeof(console) == "undefined") { console = {}; } 
if (typeof(console.log) == "undefined") { console.log = function() { return 0; } }
问题回答

response.session.access_token doesn t work in my code. But this works: response.authResponse.accessToken

     FB.login(function(response) { alert(response.authResponse.accessToken);
     }, {perms: read_stream,publish_stream,offline_access });

www.un.org/Depts/DGACM/index_spanish.htm 如果你已经连接了,那么在javascript console,就简单地照样:

FB.getAuthResponse()[ accessToken ]

window.fbAsyncInit = function () {
    FB.init({
        appId:  Your-appId ,
        cookie: false,  // enable cookies to allow the server to access 
        // the session
        xfbml: true,  // parse social plugins on this page
        version:  v2.0  // use version 2.0
    });
};

// Load the SDK asynchronously
(function (d, s, id) {
    var js, fjs = d.getElementsByTagName(s)[0];
    if (d.getElementById(id)) return;
    js = d.createElement(s); js.id = id;
    js.src = "//connect.facebook.net/en_US/sdk.js";
    fjs.parentNode.insertBefore(js, fjs);
}(document,  script ,  facebook-jssdk ));

   
function fb_login() {
    FB.login(function (response) {

        if (response.authResponse) {
            console.log( Welcome!  Fetching your information....  );
            //console.log(response); // dump complete info
            access_token = response.authResponse.accessToken; //get access token
            user_id = response.authResponse.userID; //get FB UID

            FB.api( /me , function (response) {
                var email = response.email;
                var name = response.name;
                window.location =  http://localhost:12962/Account/FacebookLogin/  + email +  /  + name;
                // used in my mvc3 controller for //AuthenticationFormsAuthentication.SetAuthCookie(email, true);          
            });

        } else {
            //user hit cancel button
            console.log( User cancelled login or did not fully authorize. );

        }
    }, {
        scope:  email 
    });
}
<!-- custom image -->
<a href="#" onclick="fb_login();"><img src="/Public/assets/images/facebook/facebook_connect_button.png" /></a>

<!-- Facebook button -->
<fb:login-button scope="public_profile,email" onlogin="fb_login();">
                </fb:login-button>

window.fbAsyncInit = function () {
    FB.init({
        appId:  Your-appId ,
        cookie: false,  // enable cookies to allow the server to access 
        // the session
        xfbml: true,  // parse social plugins on this page
        version:  v2.0  // use version 2.0
    });
};

// Load the SDK asynchronously
(function (d, s, id) {
    var js, fjs = d.getElementsByTagName(s)[0];
    if (d.getElementById(id)) return;
    js = d.createElement(s); js.id = id;
    js.src = "//connect.facebook.net/en_US/sdk.js";
    fjs.parentNode.insertBefore(js, fjs);
}(document,  script ,  facebook-jssdk ));

   
function fb_login() {
    FB.login(function (response) {

        if (response.authResponse) {
            console.log( Welcome!  Fetching your information....  );
            //console.log(response); // dump complete info
            access_token = response.authResponse.accessToken; //get access token
            user_id = response.authResponse.userID; //get FB UID

            FB.api( /me , function (response) {
                var email = response.email;
                var name = response.name;
                window.location =  http://localhost:12962/Account/FacebookLogin/  + email +  /  + name;
                // used in my mvc3 controller for //AuthenticationFormsAuthentication.SetAuthCookie(email, true);          
            });

        } else {
            //user hit cancel button
            console.log( User cancelled login or did not fully authorize. );

        }
    }, {
        scope:  email 
    });
}
<!-- custom image -->
<a href="#" onclick="fb_login();"><img src="/Public/assets/images/facebook/facebook_connect_button.png" /></a>

<!-- Facebook button -->
<fb:login-button scope="public_profile,email" onlogin="fb_login();">
                </fb:login-button>




相关问题
Facebook Connect login dialog not working

I am using Facebook Connect for iPhone and following the official instructions. I use the following code to display the login dialog: FBLoginDialog* dialog = [[[FBLoginDialog alloc] initWithSession:...

Facebook App Profile Tab is Empty ... No Content Displayed?

I can view my application via the http://apps.facebook.com/myapplication/ link and the content shows up correctly. I also added the application as a tab to a facebook page. However, when viewing the ...

Facebook Platform error: "Object cannot be liked"

I m working on a Facebook Application that generates wall posts. In testing these posts, I ve discovered that the Facebook Platform action of "liking" a post is failing. The specific error message ...

how to call showPermissionsDialog() in php (facebook api)?

I was reading over the documentation yet I could not figure out how to call Facebook.showPermissionsDialog() in php include_once ./facebook-platform/php/facebook.php ; $facebook = new Facebook(my ...

Facebook connect

If I plug in the facebook connect into my website, How can I prevent double signups? Lets say I have a user that s already signed up to my site but he clicked the connect with facebook, is there a ...

热门标签