Facebook的JavaScript SDK
我想获得facebook的访问令牌,我从facebook开发者网站获得了此代码,但它无法生成或给我任何访问令牌。我在一个简单的文本文件中使用此代码,而不是我的任何网站Facebook的JavaScript SDK,javascript,facebook,facebook-graph-api,facebook-javascript-sdk,Javascript,Facebook,Facebook Graph Api,Facebook Javascript Sdk,我想获得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
<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>
window.fbAsyninit=函数(){
FB.init({
appId:'377263832388887',//应用ID
状态:true,//检查登录状态
cookie:true,//启用cookie以允许服务器访问会话
xfbml:true//解析xfbml
});
FB.Event.subscribe('auth.authResponseChange',函数(响应){
如果(response.status===“已连接”){
//用户已登录并已验证您的身份
//app和response.authResponse提供
//用户ID、有效访问令牌、签名
//请求,以及访问令牌的时间
//和签名的请求都将过期
var uid=response.authResponse.userID;
var accessToken=response.authResponse.accessToken;
//TODO:处理访问令牌
}else if(response.status===“未授权”){
//用户已登录到Facebook,
//但尚未验证您的应用程序
}否则{
//用户未登录到Facebook。
}
};
//异步加载SDK
(职能(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);
}(文件);
});
我想获得访问令牌,以便可以继续我的工作。谢谢。请考虑使用 将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
}
});
虽然我在上面看到FB.Event.subscribe的结果数组中也应该有访问代码。您是否尝试将“响应”转储到控制台以查看结果?我想构建应用程序或通过c爬网我自己的状态#为此,我需要访问令牌,对于访问令牌,我需要在javaScript SDK中编写代码,我不知道它的功能也是javaScript SDK的一部分。您是否尝试过转储“响应”以查看您得到了什么?对不起,我帮不了你。也许如果你在你的问题上加上一个C#标记,其他人可能会帮助你。看起来您可能能够对服务器端进行身份验证。我只需要只处理JavaScript SDK的访问代码,当我执行此代码时,web浏览器将不返回任何内容。我编辑了答案以更好地反映这一想法。您将使用原始问题中提供的相同代码,但替换FB.Event.subscribe函数。此外,我建议您在新功能中的注释位置转储结果,以便更好地了解问题所在(如果有)。您是否真的要求用户授权您的应用程序?没有它,您将无法获得访问令牌。