gpt4 book ai didi

javascript - 为什么 Graph API 在第一次登录时返回我相册的问号封面照片,而在后续登录时返回正确的图像?

转载 作者:数据小太阳 更新时间:2023-10-29 04:46:16 24 4
gpt4 key购买 nike

我正在使用 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}}

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

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

最佳答案

我不确定,但我想您还没有订阅authResponseChange event .

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

<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>

希望对您有所帮助,祝您好运!

关于javascript - 为什么 Graph API 在第一次登录时返回我相册的问号封面照片,而在后续登录时返回正确的图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20139858/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com