Why is the Facebook API returning an empty array for albums?
I'm fairly ne开发者_开发知识库w to the Facebook API, but I've tried to do my due diligence to figure this out, but I can't seem to. I'm simply trying to get a list of the logged-in user's albums, using various techniques, to no avail. I'm using the JavaScript SDK, and I have the following code:
<script src="http://connect.facebook.net/en_US/all.js"></script>
<script>
FB.init({
appId:'{MY_APP_ID}',
cookie: true,
status: true,
xfbml: true,
oauth : true
});
FB.login(function(response) {
if(response.status === 'connected') {
FB.api('/me/albums', function(response) {
console.log(response);
});
}
}, { scope : 'user_photos' });
</script>
Note that {MY_APP_ID} is actually my application ID. So, when this executes, the popup will request access to the users photos, and that works fine. Now, in code, response returns a data
array that contains no elements. In addition to trying the above, I've tried initiating a JSON request to the following URL as well as directly visiting it in the browser:
https://graph.facebook.com/me/albums?access_token=" + response.authResponse.accessToken
And this returns an empty data array as well. The access token that I'm using is coming either from the FB.login method or FB.getLoginStatus. Any help would be greatly appreciated. Thanks in advance.
So, I figured it out, so I will answer my own question. All the other answers on SO was related to permissions, but this wasn't it. It turns out that if I looked at my account settings (I'm the 'user'), and looked at apps, for this application, for some reason, it showed it only was requesting the email address. I knew this wasn't correct, because I was clearly asking for user_photos as well. So, after deleting the application from my profile, and reinitiating the connect, it looks like I'm able to access the albums fine. I'm not sure if Facebook intended to do this by design, but it seems like a potential bug to me.
This can also happen if you're using the JS SDK, as that authentication process and auth dialog are apparently complete separate from what you set up in the developer app.
You need to ALSO include the permissions you desire when you call FB.login() using the optional scope parameter, eg...
FB.login(function(response) {
if (response.authResponse) {
M.FB.setIsAuthorised(true);
// On success.
} else {
// On failure.
}
}, {
scope: 'email,friends_birthday,user_photos,friends_photos,publish_actions'
});
I had a similar issue. Several solutions on stackoverflow mostly attributed the issue to the lack of required permissions. Check the permissions you need by using the Graph API explorer or documentation (The tables have a column called permissions for each field).
Jonathan's solution actually gave me a clue about where I went wrong. I had a different issue where I had double-checked all the permissions set for my app dashboard that were correct, but while logging in, I had specified a scope with a set of permissions which seemed to override the app settings. :( I found this strange, but I rectified it by including the other permissions in the scope too and my problem was resolved.
An effective way to see if the right permissions are being set through the code is to use the following code:
FB.api({ method: 'fql.query', query: 'SELECT user_status,friends_status,user_photos,friends_photos,user_location,friends_location FROM permissions WHERE uid=me()' }, function(resp) {
for(var key in resp[0]) {
if(resp[0][key] === "1")
console.log(key+' is granted');
else
console.log(key+' is not granted');
}
});
P.S: Also make sure that if you changed the permissions in the app dashboard, you log out of your application and login again before testing.
I wasted over 3 hours trying to fix this issue and facebook documentation did not help me a bit, so hope this helps someone!
精彩评论