我正在使用 phonegap facebook 插件和 facebook javascript api。这是我的代码

FB.init({
    appId: "xxxxxxxxxxxxxxx",
    status: true,
    oauth :true,
    nativeInterface: CDV.FB,
    //channelURL : 'www', // Channel File
    cookie: true,
    //useCachedDialogs: false,
    xfbml: true
});

FB.login(function(response) {
    if (response.authResponse) {
        alert(response.authResponse.userID); // literally shows three dots
        FB.api('/me', function(me){
            if (me.id) {
                alert(me.id);  // this works
            }
        });
    }
}, { scope: "email" });

我可以从 authResponse 获取 accessToken .... 它是一个长字符串。但 userID 字段字面意思是“...”。我可以通过使用另一个图形 API 调用到服务器进行额外的往返来获取用户的 ID,以获取 /me 但这似乎很浪费。

最佳答案

[为了OP的利益]您可以在开始显示时通过对服务器进行额外调用来获取客户端中的用户ID。这是正确的代码:

    FB.api('/me', function(me){
        if (me.id) {
            var facebook_userid = me.id;
            alert(facebook_userid);
        }
    });

这并不能解释为什么登录响应对象中的 userID 字段是 "..."。但这是一种解决方法。

关于javascript - 无法从 facebook javascript login api 获取用户 ID,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10702307/

10-15 03:22