最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Facebook get access Token - Stack Overflow

programmeradmin7浏览0评论

How to get a "access token" using Facebook Graph API. I have app ID, the username and password of the user. Just i need to get the access token so that i may be able to access news feed,.... and others.

How to get a "access token" using Facebook Graph API. I have app ID, the username and password of the user. Just i need to get the access token so that i may be able to access news feed,.... and others.

Share Improve this question edited Feb 21, 2012 at 11:16 ifaour 38.1k12 gold badges73 silver badges79 bronze badges asked Feb 23, 2011 at 8:47 SNRSNR 1,2492 gold badges20 silver badges39 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 18

Okay so you need to get the access_token of a user in JavaScript. Here you go:

  1. Start reading the JavaScript SDK documentation
  2. Something like the below will get you started:

    <div id="fb-root"></div>
    <script>
    window.fbAsyncInit = function() {
        FB.init({
            appId      : 'YOUR_APP_ID', // App ID
            channelUrl : '//WWW.YOUR_DOMAIN.COM/channel.html', // Channel File
            status     : true, // check login status
            cookie     : true, // enable cookies to allow the server to access the session
            xfbml      : true  // parse XFBML
        });
        // this shouldn't be called directly, but instead should be initiated with a user click event
        FB.login(function(response) {
            if (response.authResponse) {
                console.log('Access Token: ' + response.authResponse.accessToken);
            } else {
                console.log('User cancelled login or did not fully authorize.');
            }
        });
    
    };
    
    // 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/en_US/all.js";
        ref.parentNode.insertBefore(js, ref);
    }(document));
    </script>
    
  3. As mentioned in the documentation:

    Calling FB.login results in the JS SDK attempting to open a popup window. As such, this method should only be called after a user click event, otherwise the popup window will be blocked by most browsers.

  4. NEVER EVER ask for the user password nor give it to ANY application if it asks you! and if it does, report it to Facebook directly!

  5. It's "JavaScript" not "Java Script" :-)

发布评论

评论列表(0)

  1. 暂无评论