Getting access token for CURL/Browser Graph API - facebook

I'm trying to obtain messages and attachments from Facebook, I'm using:
https://api.facebook.com/method/message.getThreadsInFolder?access_token=AT&folder_id=0
To get the list of threads and:
https://graph.facebook.com/fql?access_token=AT&q=QUERY
For my queries.
But, every time, I copy the access token generated by the Graph API Explorer as I don't seem to be able to get it programmatically:
https://graph.facebook.com/oauth/access_token?grant_type=client_credentials&client_id=ID&client_secret=SECRET
Gives me an access token, but it doesn't work. If I try to use it with the URLs above I get:
"error_msg":"A session key is required for calling this method"
And for the second one:
"message": "Invalid OAuth access token.",
"type": "OAuthException",
"code": 190
The Facebook documentation it's really unclear.
I do not want to use a login flow, is there a way to login programmatically with the stored credentials as the mobile application does?

You need a login flow which you are not using (https://developers.facebook.com/docs/facebook-login/). It is not possible to get a user access token programmatically. This defeats the point of manual user interaction. The user must explicitly grant access to your application.

Related

Retrieve Lead Ads from facebook graph with App Token

I'm trying to retrieve Facebook Lead using graph API using App Token to be long time token.
But the graph API return the following error
{
"error": {
"message": "A user access token is required to request this resource.",
"type": "OAuthException",
"code": 102,
"fbtrace_id": ""
}
}
I want to retrieve it with app token, any ideas.
Thanks in advance.
I have found solution. If you need to retrieve page leadgen then you must use page access token where ad was created and generated, otherwise it doesn't work.
How I have solved this case:
Connect to application and logging in with user
Exchange retrieved user short (1-2h) access token to long lived access token (~60 days if not using access token, else if you use it daily it will never expire)
call facebook api to retrieve user pages thru server using new users long lived token (this way page access tokens will have ~60 days and if use it daily then it will never expire)
when webhook sends you leadgen_id use server call to retrieve lead data using not app access token but long lived page access token that you have saved in database. As webhook sends leadgen_id, page_id and etc, you can connect page access token to page id and then with webhook data you can get correct access token and retrieve details about the lead from facebook.
For me this approach worked. I'm using Python to communicate in server side.
Comment or message me if you'r having issues.

Facebook open graph says access_token needed

I'm trying to post the following the facebook Graph API explorer.
cody.short.94/notifications? access_token= {User Access Token} &template= this is a test &href= www.test.com
I want to be able to send the user a notification, but all attempts have failed. I'm currently getting this error:
{
"error": {
"message": "(#15) This method must be called with an app access_token.",
"type": "OAuthException",
"code": 15
}
}
I'm not sure what is causing it, and I'm in need of some guidance. Any help would be appreciated. note: my app is currently marked for web so that it not the problem
See, the error is quite self-explanatory :
This method must be called with an app access_token
The Notifications API use the APP Access Token, not the user access token!
The App Access Token is: APP_ID|APP_SECRET or get directly from here for testing. (Make sure of the security of this token, this is very crucial and just like a password to your app)
Now test the query with Graph API Explorer , and remove the user token from the Access token field and replace it with the App Access Token
It will look something like this-
Make sure you paste the app access token APP_ID|APP_SECRET on the top field that says Access Token

Getting App Access Token for facebook app?

I have read this:
trying to get app access token
And it doesn't work...
I'm getting the following error:
"error": {
"message": "An active access token must be used to query
information about the current user.",
"type": "OAuthException",
"code": 2500
}
I need the app access token in order to create open graph objects that are owned by the application.
I know how to create objects owned by the users, but I just cant find the correct way of creating the App access token.
AppId -> doesn't work...
AppId|AppSecret -> doesn't work....
App ClientToken -> doesn't work....
Anyone know this?
You can get it directly from the Access Token Tool.
Please note: For security, app access token should never be hard-coded into client-side code, doing so would give everyone who loaded your webpage or decompiled your app full access to your app secret, and therefore the ability to modify your app. This implies that most of the time, you will be using app access tokens only in server to server calls.
It looks like you have a valid token, you're just trying to query for the current user. If you're using the app token, however, there isn't a current user, so it's failing.
If you are using Java language to fetch app access token, you can try this:
DefaultFacebookClient facebookClient = new DefaultFacebookClient(Version.LATEST);
AccessToken accessToken = facebookClient.obtainAppAccessToken(clientId, clientSecret);
System.out.println(accessToken);
To check whether it is valid, use access token debugger.

How to Get feed of public Facebook page using Graph api?

There are already several questions on StackOverflow ( Can you get a public Facebook page's feed using Graph API without asking a user to allow? Get public page statuses using Facebook Graph API without Access Token? )concerning my question: I need to get through the graph API the feeds of a public page on facebook.
I followed the instructions but I always get:
{"Error": {"message": "An access token is required to request this resource.", "Type": "OAuthException", "code": 104}}
how can I fix this? I'm going crazy because everyone seems to work except me! thanks
Based on your description, the error is that you weren't supplying a valid access token with your request.
If the app's type is set to 'Native / Desktop' it's assumed you distributed the app's secret key with the binary, and thus the app access token isn't trusted (and 'getAccessToken' in the PHP SDK will only work when real users log in, it can't fall back to the app token)
If you were attempting to use the app access token to read the page's feed, this would fail in the manner you described.

Help understanding how to integrate the FB Graph API in my App

I'm working to enable my app to hit the FB Graph API to look up data on a user. I want to do this in a way that is app focused meaning it isn't based on a user signed into my app. It's a relationship between the app & the FB Graph API.
My understanding is I can do that with the APPLICATION ACCESS TOKEN is that right?
If I use my app access token here:
https://graph.facebook.com/search?q=born#gmail.com&type=user&access_token=208131059208382|xFN8da1tIDxuuMp8hoRtGs3l0aM
I get an error:
{
"error": {
"type": "OAuthException",
"message": "An access token is required to request this resource."
}
}
Why is that?
Can I use my personal FB account to hit the API for the purposes of the app?
When a user oAuth's in to an app, how long does the access token last? Is it something I can hard code in my app so I can hit the FB graph api?
Thanks
For a lot of things on Facebook, you have to have a valid session access token from the user. What this means is, the access token you get from a user must be used to access a resource. This is especially true when pulling data about the user out of the graph. The access token that FB gives you when the user loads your application typically lasts for an hour (if I recall correctly). The session object FB gives you tells you the expire time on the token, though.
If you need to access the data for a user in an offline manner, you could ask the user for the offline_access extended permission. Doing this makes Facebook give you an indefinite access token, which doesn't ever expire (unless the user changes their password). That allows you to query the graph on the user's behalf whenever you feel like doing it.
The search feature doesn't require an access token if you're performing a standard search, or a page search, etc. But it does require a valid access token when you're searching for users. I assume this is to keep people from hammering the search and crawling public user data with a bot.
You can follow the instruction in
http://developers.facebook.com/docs/authentication/
take a look at https://github.com/michaelbaldry/fb_graph_canvas - It's a barebones canvas application that you can test locally. It uses fb_graph and is about as simple as can.