I've been experimenting with the Facebook API using the Javascript SDK. Whenever I'm logged into Facebook I can pretty reliably call:
function init() {
// Called after loading the Facebook SDK
FB.api("/me", function(response) {
console.log(response.first_name); // "John" (always works)
});
}
But I also want to get a users photo albums (when they click a link) and for some reason this call only works intermittently:
$(document).ready(function) {
$("#get-albums-link").click(function() {
var accessToken = "456"; // Just an example
FB.api("/me/albums?access_token=" + accessToken, function(response) {
console.log(response); // An empty object about 40% of the time
});
});
});
When this happens I can open up a new tab and verify that even direct queries return this same empty result (note that although this could be an authorizations issue, there is no obvious indication that it is; it is simply an empty JSON object):
<!-- An HTTPS GET at https://graph.facebook.com/me/albums?access_token=456 -->
{
"data": [
]
}
As Felipe pointed out above, the access token can be the issue, as not having permissions set will return {"data":[]}. Try it yourself: https://developers.facebook.com/tools/explorer/?method=GET&path=me%2Falbums
Also, even if it is a valid auth_token, the SDK doesn't require one, so don't use it. You might end up with something like graph.facebook.com/me/albums?access_token=123423&access_token=981234 (second access token added by SDK). I, personally, don't know how the FB.api function is built, but just follow the docs: https://developers.facebook.com/docs/reference/javascript/FB.api/
Let me know if you still have issues.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With