I want to get Facebook statistics about campaigns and ads using the Facebook Ads API (https://github.com/facebook/facebook-php-ads-sdk).
I have created an app (with ID and secret), added the Marketing API and now I can generate an access token with the ads_read
permission, which is valid for 2 month.
Is there any way that I can avoid the process of manually clicking that button every other month?
I have tried this call:
$tokenUrl = 'https://graph.facebook.com/v2.8/oauth/access_token?client_id=' . $app_id . '&client_secret=' . $app_secret . '&grant_type=client_credentials&scope=ads_read';
This gives me an access token that look something like this: 680012345678976|aIDrLVvFqHXDzl6538RLUF4S9C4
and it doesn’t work (Unsupported get request).
The manually generated token is 164 chars long.
2
Answers
You can use the short living token for a long living one (even without expiration) by doing a request from your server
Note that even the token that is set to never expire can be invalidated by user, for example if he changes FB password or removes your app entirely.
For more details refer to the docs
What I did was:
1.Call
then after clicking “Allow”, I would get redirected to
2.Exchange the authorized code for access token
your redirect uri should ends with / e.g. http://localhost:3000/
I tried this by manually copied and pasted in a browser, but automate request/response should give the same result.
Reference: https://developers.facebook.com/docs/marketing-api/access