简体   繁体   English

适用于Facebook的JavaScript SDK

[英]JavaScript SDK for Facebook

I want to get the access token for facebook and I got this code from facebook developer website but it can't generate or give me any access token . 我想获取Facebook的访问令牌,我从Facebook开发者网站上获取了此代码,但是它无法生成或给我任何访问令牌。 I use this code in a simple text file not any of my website. 我在一个简单的文本文件(而不是我的网站)中使用此代码。

<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>

I want to get access token so that I can further proceed for my work. 我想获取访问令牌,以便我可以继续进行工作。 Thank you. 谢谢。

consider using this code provided by http://developers.facebook.com/docs/reference/javascript/FB.getLoginStatus/ 考虑使用http://developers.facebook.com/docs/reference/javascript/FB.getLoginStatus/提供的代码

replace the FB.Event.subscribe function with the following. 用以下内容替换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
  }
  });

Although I see on http://developers.facebook.com/docs/reference/javascript/FB.Event.subscribe/ that the FB.Event.subscribe should also have the access code in its result array. 尽管我在http://developers.facebook.com/docs/reference/javascript/FB.Event.subscribe/上看到FB.Event.subscribe的结果数组中也应该具有访问代码。 Have you tried dumping the 'response' to console, to view the results? 您是否尝试过将“响应”转储到控制台以查看结果?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM