あなたの最善の策は、呼び出して代わり割り当ての関数内でこの変数を設定するには、次のようになります。
(function(w) {
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
// the user is logged in and connected to 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
w.fb_user_id = response.authResponse.userID;
w.accessToken = response.authResponse.accessToken;
} else if (response.status === 'not_authorized') {
// the user is logged in to Facebook,
//but not connected to the app
} else {
// the user isn't even logged in to Facebook.
}
});
})(window);
その後、後に自分のページ内(呼び出しはすぐにこの1に依存していない提供)、することができますページ上の変数にグローバル変数fb_user_id
(window.fb_user_id
,
とも呼ばれます)を使用してコードにアクセスします。 1.5では、jQuery Deferredsを使用して同期性の問題を解決することもできます:
var getUserId = function() {
return $.Deferred(function(d) {
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
// the user is logged in and connected to 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 auth = response.authResponse;
d.resolve(auth.userID, auth.accessToken);
} else if (response.status === 'not_authorized') {
// the user is logged in to Facebook,
//but not connected to the app
d.reject();
} else {
// the user isn't even logged in to Facebook.
d.reject();
}
});
}).promise();
};
getUserId()
.done(function(userId, token) {
// userId and token are available in here
})
.fail(function() {
// run some code in here if not authorized
// or something else failed;
});
出典
2012-03-05 00:56:45
Eli
そして、どうすればこの機能のw.fb_user_idを使うことができますか? –
私はアクセスできません。私は "console.log(fb_user_id);しようとしました"と私は間違ったエラーを得た。これはタブページで動作するFacebookブックアプリです。これは何にも影響しますか? –
私が言ったように、このメソッドを使用すると、_asynchronously_に設定されているため、すぐにユーザーIDにアクセスすることができなくなります。私は別の代替のためのコールバックメカニズムを提供します。 – Eli