[Javascript/PHP] Retrieve Facebook feed - sdk 4.0

Started by Apidcloud, November 23, 2014, 06:05:32 am

Previous topic - Next topic

Apidcloud

November 23, 2014, 06:05:32 am Last Edit: November 23, 2014, 12:22:07 pm by Apidcloud
Hello,

I've been trying to retrieve someone's facebook feed using both php and javascript but nothing has worked so far. According to what I've tried, a session is needed to retrieve an access token and therefore be able to see someone else's publicly feed - https://developers.facebook.com/docs/facebook-login/access-tokens/#usertokens

If we try the following url: https://graph.facebook.com/PAGE-ID/feed it returns an error saying that an access token is required to request this resource.

Below's my javascript attempt, based on the facebook sdk documentation:
http://pastebin.com/2KkU0iyF

Thing is, the /PAGE-ID/feed is returning an empty array. I've tried to ask for something like /me and it prints basic information in the console, like name, id, username and so on.

Console output:
Objectdata: Array[0]
__proto__: Object


I believe I'm missing something simple like permissions or so (I tried to use scope in the login phase). However, according to the Graph API documentation (https://developers.facebook.com/docs/graph-api/reference/v2.2/post/),
any valid access token should work if the post is public.


PHP attempt: http://pastebin.com/4wCBqSTu

First things first. Do I really need to ask for login here? I only want to read the public posts. Isn't app-id and app-secret enough to generate the session?
According to the documentation, the /feed returns an array of post objects.

However, when I use /PAGE-ID/feed the return is an empty array: [backingData:protected] => Array ( )

If I use /PAGE-ID only, it prints the basic information, such as username, name, etc..


Any suggestions?

Thanks
Instead of wanting to be somebody else, rather become somebody else



"I will treasure the knowledge like a squirrel treasures acorns."


Gibbo Glast 2D Engine - The sky is no longer a limit

Blizzard

It might depend on the privacy settings. You most probably do need permissions and a session. I only know that in Java on Android and Objective-C on iOS you need to start a session and the user has to allow the application to access certain data so they do need a login there. When trying it on the web, it might be different, I don't know that. But even if you're logged in with the Facebook app, you still need to give the app (as the user) access to your profile, etc. (whatever permissions that app is asking for). So try it with a login.
Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.

Apidcloud

"Even for public posts, you MUST use a User Token for the specific User, and you need to authorize him with the read_stream permission."

So yes, you were totally right. Besides the login I also had to ask for a specific permission. That way the login itself is working, but I'm still struggling with the returned data  :facepalm:
Instead of wanting to be somebody else, rather become somebody else



"I will treasure the knowledge like a squirrel treasures acorns."


Gibbo Glast 2D Engine - The sky is no longer a limit

Apidcloud

Sorry for the double posts - just going to post the answer.

Seems I wasn't understanding well what I actually wanted  :wacko:

So, if one wants to read the feed from a user's profile, then yes, the Login is a must in order to generate the User Token which is handled by the sdk internally. Lastly, since the information has different privacy levels, to retrieve someone's feed, one must ask the permission to do so. That's achieved by adding read_stream to the Login scope.

If however, one wants to read a public page's feed, which is what I wanted since the beginning, then there's no need for a Login and consequently no need for adding more permissions. The reason why it works in this case is simple. If the page feed has no restrictions such as gender or age, then the App Token which is included by default in every call does suffice.

Example for the first case (user's feed):
http://pastebin.com/8WzmzxX0

Example for the second case (page's feed):
http://pastebin.com/wKcfB8vj

Further reading:
https://developers.facebook.com/docs/facebook-login/access-tokens/
http://www.devils-heaven.com/facebook-access-tokens/

Thanks
Instead of wanting to be somebody else, rather become somebody else



"I will treasure the knowledge like a squirrel treasures acorns."


Gibbo Glast 2D Engine - The sky is no longer a limit

Blizzard

Yes, you got it now. I didn't know the exact details, but I what you just said is exactly what I was trying to tell you to check on. :)
Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.