2011-02-23 188 views
4

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

+0

您正在使用什么语言? – ifaour 2011-02-23 10:10:43

+0

java脚本... – SNR 2011-02-23 10:56:13

回答

18

好的,你需要在JavaScript中获得用户的access_token。在这里你去:

  1. 开始阅读JavaScript SDK documentation
  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. 正如documentation提到:

    调用FB。登录结果JS SDK试图打开弹出式窗口 。因此,只有在用户点击 事件后才能调用此方法,否则弹出窗口将被大多数浏览器阻止。

  4. NEVEREVER要求用户输入密码或如果它要求你把它给任何应用!如果有,请直接向Facebook报告!

  5. 这是 “JavaScript的” 而不是 “Java脚本” :-)

+3

+1为拼写更正:-) – 2011-08-26 07:11:07

+2

+1提及Popup块! – hhh 2012-06-05 23:55:42

+0

@hhh,欢迎您......我也编辑了代码,并在FB.login上面添加了一条评论,仅供复制/粘贴代码的人阅读完整答案。 ;-) – ifaour 2012-06-06 07:32:53