Sov*_*iut 7 javascript facebook oauth facebook-graph-api facebook-javascript-sdk
我正在使用Graph API的FB javascript驱动程序,允许用户从他们的Facebook帐户中挑选照片.他们第一次连接时会提示他们登录:
FB.login(function(res) {
if (res.status == 'connected') {
auth = res.authResponse; // cache auth response
getAlbums();
}
});
Run Code Online (Sandbox Code Playgroud)
如果成功,我会缓存返回的auth对象并立即获取用户的相册:
function getAlbums() {
FB.api('/me/albums', function(res) {
albums = res.data;
});
}
Run Code Online (Sandbox Code Playgroud)
使用返回的对象,我遍历相册并显示以下内容cover_photo:
https://graph.facebook.com/{{album.cover_photo}}/picture?type=normal&access_token={{auth.accessToken}}
Run Code Online (Sandbox Code Playgroud)
用户第一次登录时,所有封面照片都是问号图标.但是,如果用户刷新,或返回页面,应用程序的重新认证,识别用户已经登录,并且显示适当的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>
Run Code Online (Sandbox Code Playgroud)
希望它有帮助,祝你好运!
| 归档时间: |
|
| 查看次数: |
768 次 |
| 最近记录: |