为什么Graph API会在首次登录时返回我相册的问号封面照片,并在后续登录时返回正确的图像



我使用Graph API的FB javascript驱动程序,允许用户从他们的Facebook帐户中挑选照片。他们第一次连接时,会提示他们登录:

FB.login(function(res) {
    if (res.status == 'connected') {
        auth = res.authResponse; // cache auth response
        getAlbums();
    }
});

如果成功,我缓存返回的auth对象,并立即使用获取用户的相册

function getAlbums() {
    FB.api('/me/albums', function(res) {
        albums = res.data;
    });
}

使用返回的对象,我迭代相册,并用显示它们的cover_photo

https://graph.facebook.com/{{album.cover_photo}}/picture?type=normal&access_token={{auth.accessToken}}

用户第一次登录时,所有封面照片都是问号图标。但是,如果用户刷新或返回页面,应用程序会重新验证,识别用户已经登录,并显示正确的封面图片缩略图。

如何让新认证的用户能够看到他们的封面照片?

我不确定,但我认为您尚未订阅authResponseChange事件。

下面的一段简单代码与您正在寻找的内容相同-

<html>
<head></head>
<body>
<div id="fb-root"></div>
<script>
var access_token = "";
window.fbAsyncInit = function() 
{
    FB.init({
        appId      : '<APP-ID>',
        status     : true,
        cookie     : true,
        xfbml      : true 
    });
    FB.Event.subscribe('auth.authResponseChange', function(response) 
    {
        access_token = response.authResponse.accessToken;
        if (response.status === 'connected') 
        {
            FetchUserPhotos();
        } 
        else if (response.status === 'not_authorized') 
        {
            FB.login('scope: user_photos');
        } 
        else 
        {
            FB.login('scope: user_photos');
        }
    });
};
// 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));
function FetchUserPhotos() 
{
    FB.api('/me/albums', function(response) 
    {
        var albums = response.data;
        for(var i=0; i<albums.length; i++)
        {
            var album = albums[i];
            if(album.cover_photo != undefined)
            {
               FB.api("/"+album.cover_photo+"?access_token="+access_token, function(cover_photo)
               {
                   console.log(cover_photo.source);
               });       
               //console.log("https://graph.facebook.com/"+album.cover_photo+"/picture?type=normal&access_token="+access_token);
            }
         }          
    });
}
</script>
<fb:login-button scope="user_photos" show-faces="true" width="200" max-rows="1"></fb:login-button>
</body>
</html>

希望有帮助,祝你好运!

最新更新