0

我想获得 facebook 的访问令牌,我从 facebook 开发者网站获得了这段代码,但它无法生成或给我任何访问令牌。我在一个简单的文本文件中使用此代码,而不是我的任何网站。

<div id="fb-root"></div>
<script>
<div class="fb-login-button" data-show-faces="true" data-width="400" data-max-rows="1"></div>
  window.fbAsyncInit = function() {
    FB.init({
      appId      : '377263832388887', // App ID
      status     : true, // check login status
      cookie     : true, // enable cookies to allow the server to access the session
      xfbml      : true  // parse XFBML
    });
FB.Event.subscribe('auth.authResponseChange', function(response) {
    if (response.status === 'connected') {
        // the user is logged in and has authenticated your
        // app, and response.authResponse supplies
        // the user's ID, a valid access token, a signed
        // request, and the time the access token 
        // and signed request each expire
        var uid = response.authResponse.userID;
        var accessToken = response.authResponse.accessToken;

        // TODO: Handle the access token

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

我想获得访问令牌,以便我可以进一步进行我的工作。谢谢你。

4

1 回答 1

0

考虑使用http://developers.facebook.com/docs/reference/javascript/FB.getLoginStatus/提供的代码

将 FB.Event.subscribe 函数替换为以下内容。

FB.getLoginStatus(function(response) {
  if (response.status === 'connected') {
    var uid = response.authResponse.userID;
    var accessToken = response.authResponse.accessToken;

    // dump success data here, and dump response data if needed

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

    // dump fail data here

  } else {
    // dump other fail data here
  }
  });

虽然我在http://developers.facebook.com/docs/reference/javascript/FB.Event.subscribe/上看到FB.Event.subscribe 在其结果数组中也应该有访问代码。您是否尝试将“响应”转储到控制台以查看结果?

于 2012-10-19T16:12:07.713 回答