Retrieve Lead Ads from facebook graph with App Token - facebook

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.

Related

Getting videos uploaded by facebook page without requiring user to log in

I'm trying to get videos of facebook page using facebook graph api.
I get it to work when I use my personal access facebook accounts access token but if I try to use my apps token it returns
{
"error": {
"message": "A user access token is required to request this resource.",
"type": "OAuthException",
"code": 102
}
}
Is there way to get pages public videos from the api by using app token?
If not, is there way to use just my personal access token and renewing it automatically without requiring the user to login. (Server would always make the request with MY OWN access token, not their)

Getting access token for CURL/Browser Graph API

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.

Facebook session expiration error when try to post actions to open graph using 'App access token'

I am facing this error with some of my users, the case is as following:
I use facebook "App access token" to post actions to facebook Open Graph instead of the user access token because app access token don't expire according to facebook documentation unless you refreshed the app secret, I use the follwoing Post url to post actions
https://graph.facebook.com/user_facebook_id/App_Namespace:action_name?FBOG_Object=FBOG_OBJECT_URL&access_token=app_access_token
Some of the actions do appear on facebook, but for some users the actions fails to post and return the following message:
{ "error": { "message": "Error validating access token: Session has expired at unix time 1345759200. The current unix time is 1345925578.", "type": "OAuthException", "code": 190, "error_subcode": 463 } }
What am I doing wrong? Why do I have an expiration error although am using the app access token? Should I worry for user permissions?
After checking the servers it seems that the code that uses the app token was not deployed and the code that uses the user token is still there.
The app token does not expire unless the app owner took some action.
https://developers.facebook.com/docs/authentication/applications/
Error message clearly shows that your access token expired and you need to get another one.
However you can handle this issue : Access token expiration
Also if you are looking for a long life token you need to provide some more parameters while authorizing the application to a new user.
offline_access parameter can be included in a request but now it's been deprecated by facebook. Removal of offline access

How to get a Facebook access token for a page with no app or app secret

I am trying to automatically display wall posts from a Facebook page another website. I can use the Graph API explorer to get one manually. When I use the generated token in my code all is well. The problem is the tokens expire quickly. It just isn't practical to get a new code several times a day. I know there is a way to request an access token programatically - in my case via PHP, but all the examples call for an app secret. Since this is a page and not an app, there is no secret.
I have tried this:
https://graph.facebook.com/oauth/authorize?type=user_agent&client_id=MY_CLIENT_ID&redirect_uri=http%3A%2F%2FMY_SITE_URL&scope=user_status
What I get back is this:
{
"error": {
"message": "Error validating application. Cannot get application info due to a system error.",
"type": "OAuthException",
"code": 101
}
}
I have tried using Fiddler to intercept the call from the Graph API explorer to see what I need in my code file, but haven't had any luck.
Use the app-id to build this link to authorize the managing of pages
https://www.facebook.com/dialog/oauth?client_id=MY_CLIENT_ID&redirect_uri=MY_SITE_URL&scope=manage_pages&response_type=token
Exchange token for perm (longer token)
https://graph.facebook.com/oauth/access_token?client_id=MY_CLIENT_ID&client_secret=MY_CLIENT_SECRET&grant_type=fb_exchange_token&fb_exchange_token=(from link1)
Visit this page, find the PAGE you want to post and copy the new access_token
https://graph.facebook.com/me/accounts?access_token= (from link2)
Use this last token (form link3) to post to page as app
Take note as it will expire in 60 days. (FB no longer offering unlimited offline access token)
Edit (2013 Oct 24): 5th point no longer true, Page Access Tokens generated from long-lived User Tokens do not expire.
Edit (Feb 2016): Tokens now "usually" expire in 60 days but can be invalidated at any time.
For graph objects that are not private (public) you can use your app_id as an access_token.
Your app_id never changes so you don't ever have to renew it. The question is is the wall content yours?
If so you can easily use your app_id to accomplish this without having to request an access token everytime.
However, in order to have an app_id you need to create an application on facebook with a facebook developer account. As far as I understand there isnt a way to anonymously make request to the Graph API,

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.