skip to Main Content

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


  1. You can use the short living token for a long living one (even without expiration) by doing a request from your server

    GET https://graph.facebook.com/oauth/access_token?  
       grant_type=fb_exchange_token&         
       client_id={app-id}&
       client_secret={app-secret}&
       fb_exchange_token={short-lived-token} 
    

    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

    Login or Signup to reply.
  2. What I did was:

    1.Call

    https://www.facebook.com/v2.8/dialog/oauth?client_id=
    <APP_ID>&redirect_uri=<REDIRECT_URI>&scope=ads_read
    

    then after clicking “Allow”, I would get redirected to

    <REDIRECT_URI>/?code=<AUTHORIZED_CODE>
    

    2.Exchange the authorized code for access token

    https://graph.facebook.com/v2.8/oauth/access_token?client_id=<APP_ID>&redirect_uri=<REDIRECT_URI>&client_secret=<CLIENT_SECRET>&code=<AUTHORIZED_CODE>
    

    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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search