Facebook 如何访问uid+;FB.Event.subscribe之外的accesstoken?
我成功地运行了FB.Event.subscribe,并且可以从该函数中输出Facebook 如何访问uid+;FB.Event.subscribe之外的accesstoken?,facebook,facebook-graph-api,facebook-javascript-sdk,Facebook,Facebook Graph Api,Facebook Javascript Sdk,我成功地运行了FB.Event.subscribe,并且可以从该函数中输出uid和accesstoken——但是我想知道如何从基本上任何地方访问uid和accesstoken 我最初的想法是,我需要在函数之外声明一个全局uid和accesstoken变量,但是到目前为止我还没有这样做,当我试图输出全局变量时,我只得到了未定义的 我的代码就在启动标记之后: <body> <div id="fb-root"></div> <script> //Decl
uid
和accesstoken
——但是我想知道如何从基本上任何地方访问uid
和accesstoken
我最初的想法是,我需要在函数之外声明一个全局uid和accesstoken变量,但是到目前为止我还没有这样做,当我试图输出全局变量时,我只得到了未定义的
我的代码就在启动标记之后:
<body>
<div id="fb-root"></div>
<script>
//Declaring global var for uid and token
var uid;
var accessToken;
$(document).ready(function(){
function facebookReady(){
FB.init({
appId : '357646666347166', // App ID
channelUrl : '//localhost/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
});
$(document).trigger("facebook:ready");
}
if(window.FB) {
facebookReady();
} else {
window.fbAsyncInit = facebookReady;
}
});
// 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>
<script>
$(document).on("facebook:ready", null, function(){
FB.Event.subscribe('auth.authResponseChange', function(response) {
// Here we specify what we do with the response anytime this event occurs.
if (response.status === 'connected') {
// Setting the uid + token
uid = response.authResponse.userID;
accessToken = response.authResponse.accessToken;
//
}
});
// Trying to access the UID OR TOKEN HERE:
console.log(uid);
console.log(accessToken);
// Trying to access the UID OR TOKEN HERE:
});
</script>
<!-- some included js file where I also want to access the UID -->
<script src="js/animation.js"></script>
//为uid和令牌声明全局变量
变量uid;
var-accessToken;
$(文档).ready(函数(){
函数facebookReady(){
FB.init({
appId:'357646666347166',//应用ID
channelUrl:'//localhost/channel.html',//通道文件
状态:true,//检查登录状态
cookie:true,//启用cookie以允许服务器访问会话
xfbml:true//解析xfbml
});
$(document.trigger(“facebook:ready”);
}
如果(window.FB){
facebookReady();
}否则{
window.fbAsyninit=facebookReady;
}
});
//异步加载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);
}(文件);
我的代码就放在结束标记的前面,
标记-这也是我尝试访问全局uid和令牌的地方:
<body>
<div id="fb-root"></div>
<script>
//Declaring global var for uid and token
var uid;
var accessToken;
$(document).ready(function(){
function facebookReady(){
FB.init({
appId : '357646666347166', // App ID
channelUrl : '//localhost/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
});
$(document).trigger("facebook:ready");
}
if(window.FB) {
facebookReady();
} else {
window.fbAsyncInit = facebookReady;
}
});
// 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>
<script>
$(document).on("facebook:ready", null, function(){
FB.Event.subscribe('auth.authResponseChange', function(response) {
// Here we specify what we do with the response anytime this event occurs.
if (response.status === 'connected') {
// Setting the uid + token
uid = response.authResponse.userID;
accessToken = response.authResponse.accessToken;
//
}
});
// Trying to access the UID OR TOKEN HERE:
console.log(uid);
console.log(accessToken);
// Trying to access the UID OR TOKEN HERE:
});
</script>
<!-- some included js file where I also want to access the UID -->
<script src="js/animation.js"></script>
$(document).on(“facebook:ready”,null,function()){
FB.Event.subscribe('auth.authResponseChange',函数(响应){
//在这里,我们指定在发生此事件时如何处理响应。
如果(response.status===“已连接”){
//设置uid+标记
uid=response.authResponse.userID;
accessToken=response.authResponse.accessToken;
//
}
});
//尝试在此处访问UID或令牌:
控制台日志(uid);
日志(accessToken);
//尝试在此处访问UID或令牌:
});
我过去只将accessToken存储在一个全局变量中,它不存在任何问题。这里的问题是uid和accessToken仅在各自的脚本标记中具有作用域,因此在FB.Event.subscribe中声明的uid和accessToken是它们自己的值;与您的“全局变量”分开,谁的范围仅限于FB.Event.subscribe
也就是说,您只需要使用isFB.getAccessToken()
使用jQuery通过ajax请求检索数据的示例:
$.ajax({
url: "myQuery.php",
data: { accessToken: FB.getAccessToken() }
}).done(function(data) {
doStuff(data);
});
您还可以通过FB.getUserID()检索uid,这是因为JS SDK是异步工作的–当您尝试访问它时,数据尚未到达。一旦回调函数将值分配给变量,您就可以“从任何地方”访问它们,但之前不能。嗨,cbroe,非常感谢您的评论。您是否有任何机会对一种方法有任何建议,以便在我尝试在我发布的代码范围内访问它之前,能够等待它被设置?