javascript Facebook 获取访问令牌

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5088676/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 15:54:51  来源:igfitidea点击:

Facebook get access Token

javascriptfacebookfacebook-javascript-sdk

提问by SNR

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.

如何使用 Facebook Graph API获取“访问令牌”。我有应用程序 ID,用户的用户名和密码。只是我需要获取访问令牌,以便我可以访问新闻提要、.... 和其他人。

回答by ifaour

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

好的,所以您需要access_token在 JavaScript 中获取用户的 。干得好:

  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.net/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. NEVEREVERask for the user password nor give it to ANYapplication if it asks you! and if it does, report it to Facebook directly!

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

  1. 开始阅读JavaScript SDK 文档
  2. 像下面这样的东西会让你开始:

    <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.net/en_US/all.js";
        ref.parentNode.insertBefore(js, ref);
    }(document));
    </script>
    
  3. 文档中所述:

    调用 FB.login 会导致 JS SDK 尝试打开一个弹出窗口。因此,该方法只能在用户单击事件后调用,否则弹出窗口将被大多数浏览器阻止。

  4. NEVER EVER索要用户密码也不给任何应用程序,如果问你!如果是,请直接向 Facebook 报告!

  5. 它是“JavaScript”而不是“Java Script”:-)