Facebook Graph API and Generic Access token - facebook

If I understand correctly, the Graph API enables access to public pages using a generic access token. I'm finding however that many public pages are not returned in search results when using this type of token vs. a user token. I don't want users to have to go through an OAUTH dialog just to retrieve the basic properties of a public page.
This example below returns a result when using any user access token but no results when using a generic access token:
What am I doing, or understanding, incorrectly?
https://graph.facebook.com/search?q=OSullivansHerndon&type=page&access_token={myuseraccesstoken}
https://graph.facebook.com/search?q=OSullivansHerndon&type=page&access_token={APPID|APP SECRET}

You can get a generic access token for your app that will let you look at requests where you would otherwise need an access_token= parameter in the Graph call. You don't need to authenticate users as the access token is for the application.
https://graph.facebook.com/oauth/access_token?
client_id=YOUR_APP_ID&
client_secret=YOUR_APP_SECRET&
grant_type=client_credentials
example:
https://graph.facebook.com/nike/posts?access_token={response from above call}

I suspect you need a user access token to use /search as search results use social context. If you have the page id, you can load that without any access token by just hitting /id

I dont think you can get this to work without a user access token. From my limited OAUTH is the only way forward.

By specifying GRAPH API user token, you simply meant that you are using user preferences and information .
if your intention is to fetch some public page information , then you need that page userid or username . And , if you are intention is to grab all the public data which is available for your particular user , then you have to pass oAUTH system,Since indirectly you are accessing his information which is meant public under some facebook govern rules .
Accessing public page by providing user id
https://graph.facebook.com/19292868552
By writing the id of that page,you ca access its information. Now the question is how to access the id of that particular page, Manually just visit that page --> photo album . on the top of that page,you will see something like "Album Videos" . Just visit any of that link.now see,woola the id of that page is visible to you . something like this url
http://www.facebook.com/media/albums/?id=176101909092314
And btw,if you are interested in performing search , then you need to specify user token. facebook do some algorithmic search for all users and track user activities . So , the result will not always the same for you and me , as an example .

Related

Retriving data from facebook events

I have to create an webpage where i need to show all the events from the past or upcoming ones from an facebook page. I have to retrive the title, description, image, time and place of every single one and make a box with it.
After that, i have to retrive all the interested people.
I'm a beginner trying to make big things! Thank you very much.
This isnt a simple problem - I have just done this so will give you a start.
You need to use Facebooks Graph API to get the data, you will need to read up on the Graph API and how to use it. Documentation can be found here. Its worth noting due to a security breach the API isn't offering all of its services to new developers currently but you can get most - read about this here.
You can query the API using any language but I suggest do it with python and use the requests module. Alternatively you could use this SDK which makes things a lot easier, trust me! With the SDK you need to get a user access token which you can get from here.
SDK
With the SDK its possible to do this
import facebook
graph = facebook.GraphAPI(access_token="your_token", version="2.12")
event = graph.get_object(id='event_id',
fields='attending_count,declined_count')
print(event['attending_count'])
print(event['declined_count'])
You can add more fields to the fields list. However you can use any language you choose to request the data, as you can just construct your own URL's.
Graph API & Requests
This is how I did it in python using requests to query the API.
import requests, json
#Make a requests session to fetch data from URL's
session = requests.Session()
session.mount('https://', requests.adapters.HTTPAdapter(max_retries=10))
#API base URL
baseUrl = 'https://graph.facebook.com/v2.11/'
#Fields you want to get
fields = ['id', 'name', 'start_time', 'end_time', 'description', 'place']
#replace CLIENT_ID and CLIENT_SECRET with ones from a facebook app (make one)
tokenPath= 'oauth/access_token?client_id={0}&client_secret={1}&&'\
'grant_type=client_credentials'.format(CLIENT_ID, CLIENT_SECRET)
#Get the generated token
token = session.get(baseUrl + tokenPath).json()['access_token']
event = session.get(baseUrl, params={
"ids": "event_id",
"fields": ",".join(fields),
"access_token": token,
}
).json()
print(event["name"])
print(event["place"])
You can make a facebook app here which is where the CLIENT_ID & CLIENT_SECRET come from.
Why would you use URL requests and not the SDK?
Using the SDK the generated user access tokens expire every day, its possible to extend them but only for 60 days. If you want to use the SDK it has a method built in which allows you to extend the token, in order to extend the token you also need to create a facebook app. Its also possible to extend tokens using this website, though I cant vouch for its security - it could potentially access and exploit your data.
To extend the token with SDK:
import facebook
graph = facebook.GraphAPI(user_access_short_lived_token)
extended_token = graph.extend_access_token(app_id, app_secret)
print(extended_token)
Using the URL approach, you create a client access token at runtime (read about user access token vs client access token here) and the token is valid so long as your app is active. So if you are going to use this a lot or want to create it and not have to change/extend tokens then use the URL approach.
Hope this gets you started.

Facebook Graph API: Find graph object from post URL

Given the URL of a public Facebook post, how can one find the post object in the FB Graph API? (secondarily, why are so many user feeds empty or nearly empty when accessed through the API?)
We would like to be able to comment on or like a post via the v2.x
Graph API, given the post's URL. Doing so requires the post's object
ID, which we can make some educated guesses about, but accessing
the actual object through the API has proven unreliable (works for some posts but
not others).
v2 of the API introduced app-scoped user IDs, and post IDs generally seem
to be of the form {app-scoped user id}_{unique post id}. Here are the
details of some attempts to find posts in the API with various combinations of
these IDs (global user id, app-scoped user id, and post id).
Starting with a simple example: https://www.facebook.com/evan.prodromou/posts/10153023417510505. Key characteristics are that it's public, it's not a share of another post, and most importantly it has no photo(s), which add extra ids and URLs for the individual photo(s) and photo set. Evan's profile is also public, i.e. https://www.facebook.com/evan.prodromou doesn't show the generic This content is currently unavailable
Trying the bare post id from that URL, /v1.0/10153023417510505 and /v2.2/10153023417510505 both give the Unsupported get request (code 100) error.
Evan's global user id is 525575504. Attaching that as a prefix, /v1.0/525575504_10153023417510505 and /v2.2/525575504_10153023417510505 still both give the same Unsupported get request error.
Same error using his app-scoped user id instead, /v2.2/10152350676805505_10153023417510505.
OK let's try the other direction. We'll page through the user's feed until we find the post in question. It feels like this is more the expected use case for the API...
Both the global /v2.2/525575504/posts and app-scoped /v2.2/10152350676805505/posts return nothing, but /v1.0/525575504/posts returns two recent posts, this like and this post. Not sure why only those two, even though has plenty of other recent public posts. The like is fetchable via its id field in both api versions, i.e. /v1.0/525575504_10153045879215505 and /v2.2/10152350676805505_10153045879215505, but both API versions return the Unsupported get request error when fetching the post via its id field, e.g. /v1.0/525575504_351575675029953 and /v2.2/10152350676805505_351575675029953
Trying another public post, this one with pictures: https://www.facebook.com/andigalpern/posts/678121182314631 . /v1.0/100003502653187_678121182314631, /v2.2/100003502653187_678121182314631, and /v2.2/499657186827699_678121182314631 all error.
/v1.0/100003502653187/posts only includes one post, a like, and /v2.2/100003502653187/posts is empty.
For reference, here is the GitHub issue where we have been tracking this problem.
Basically you would need read_stream for this (which your app will not get granted). And yes, even though the post is public, you still need read_stream to be able to get read access to all kinds of posts.
You can easily play around with this in Graph API Explorer. First give your app read_stream permission to get ids of items in your feed – that will give you ids of the “form app-scoped user id underscore post id”. Then remove read_stream (by clicking “Get Access Token” again and using the “Clear” button), and try several of the user_* permissions – and you will see that for most of your posts, even the public ones, you will still only get “Unsupported get request”, which just means you are not allowed to read that object.
F.e., I have a public post where shared a video post from another page on my timeline, the type is video and status_type is shared_story, but neither user_status nor user_videos allow me to read this post – only when I grant read_stream again, my app can read that post. Same with another public post of type status and status_type mobile_status_update – readable with read_stream, not with any of the user_* permissions.
In short: What you want to achieve is not possible any more with API v2 and the restriction that read_stream will only be granted to apps on platforms where no official FB client exists.
A one proposition is to make api calls to write facebook posts since those return valid post_id that you could use
Second,
When you try https://developers.facebook.com/tools/explorer/256884317673197/?method=GET&path=525575504%2Fposts&version=v2.0&
You can see that his posts ID's are not starting with his public id as you wrote 525575504_*
but they still put unsupported get request
https://developers.facebook.com/tools/explorer/256884317673197/?method=GET&path=10152853212485505_351575675029953&version=v2.0
Third since above doesnt work are those posts public for sure?

Getting a user access token in facebook using Koala

I am writing a simple procedure that automatically makes a facebook
post. From what I understand, I need to have a "user access token" to
do this. I am using Koala (but the philosophy is similar for other
libraries). Anyway, I create a new OAuth account:
#oauth = Koala::Facebook::OAuth.new(app_id, app_secret, callback_url)
The koala instructions then become somewhat unclear. The next two lines are:
#oauth.url_for_oauth_code # generate authenticating URL
#oauth.get_access_token(code) # fetch the access token once you have the code
Where does the "code" variable come from? It doesn't say in the
documentation. Also, does the "get_access_token" method get an "app
access token" or a "user_access_token"? The method name is not clear.
I tried going to the url that the [url_for_oauth_code] method gave me,
but it gives me no code! Where does the "code" variable come from?
On the front page of Koala it states you need to go through the OAuth process described at http://developers.facebook.com/docs/authentication/ (this is an old link but the content within is valid)
Specifically
#oauth.url_for_oauth_code
https://github.com/arsduo/koala/blob/master/lib/koala/oauth.rb#L85
Generates a URL that you need to direct the user to based on the repo it's something like
https://www.facebook.com/dialog/oauth?
client_id={app-id}&
redirect_uri={redirect-uri}&
scope=email
Based on the documentation https://developers.facebook.com/docs/facebook-login/manually-build-a-login-flow/v2.2#login, when the response_type is omitted the default response type is code. So the above is equivalent to
https://www.facebook.com/dialog/oauth?
client_id={app-id}&
response_type=code&
redirect_uri={redirect-uri}&
scope=email
So on redirect to redirect-uri, this URL will be appended with the code param which you must handle then supply to
#oauth.get_access_token(code)
The access token is a user access token.

Facebook Graph API: find all public pages I like in a single request

I would like to know which objects returned from /me/likes are public ie. don't require authorization.
So far, I haven't found a better solution than making another request to /?ids=<IDs> without the access token, where <IDs> is a comma-separated list of IDs obtained from the previous request.
Is there any field indicating whether an object is public? Any help will be highly appreciated!
This is a matter of what you're asking for when you get the auth toke. If you have all boxes unchecked you will get just public information from the request. You'll know this by the pop-up asking for authorization saying
"Graph API Explorer will receive the following info: your public profile."
How ever if you have the box checked that says "user_likes" you'll get a list of all the likes. The authorization will say.
"Graph API Explorer will receive the following info: your public profile and likes."
If you just want to return the id of the things you like you can use this.
me?fields=likes{id}

How can I retrieve Facebook User Info without Access Token

I found a good tutorial for building Facebook membership Authentication website which demo file is here > http://niftyandcrackerjack.com/sites/default/files/Test.zip
I wonder this example does not use access token to retrieve Facebook User Info. It use classes inherits from the masterpage. As I am new to ASP.NET, I don't know how to retrieve Facebook user info. Can someone give me and suggestion or explain...... Thanks,
My finding so far indicate that basic information cannot be accessed unless one has either 'userid' or 'username' as in this post.
If one has to try and fetch basic information using following url, then access_token is needed
http://graph.facebook.com/me
Perform an HTTP GET to https://graph.facebook.com/{userid} or HTTP GET to https://graph.facebook.com/{username}
Examples: Click the link below to see the JSON response.
https://graph.facebook.com/4
https://graph.facebook.com/zuck
Pretty cool, eh? And no token needed to get at this public info.
You can get more info on this here: https://developers.facebook.com/docs/reference/api