pythonfacebook-graph-apifacebook-marketing-api

(#100) Missing permissions error in Facebook API clarification


problem description:

questions:

1.how can we generate access tokens with permissions programmatically using Graph API in python? 2.which token is advisable to extract data for ad account insights?

error message:
{
      "error": {
        "message": "(#100) Missing permissions",
        "type": "OAuthException",
        "code": 100,
        "fbtrace_id": "A4vPClDlI__dFmxxhiVVGRG"
      }
    }

access token generation code sample:

import requests

def get_fb_token(app_id, app_secret):
    url = 'https://graph.facebook.com/oauth/access_token'
    payload = {
        'grant_type': 'client_credentials',
        'client_id': app_id,
        'client_secret': app_secret
    }
    response = requests.post(url, params=payload)
    return response.json()['access_token']


app_id = 'XXXXXXXXXXXXXXX'
app_secret = 'XXXXXXXXXXXXXXXXX'

data_request = get_fb_token(app_id, app_secret)
print(data_request)

Note:


Solution

  • Simplest: If your BM has access to that ad account Use System User flow.

    1. Create A System User.
    2. Generate&Copy the long lived token from BM page and paste it into your server. (This will not expire ever)
    3. Assign the ad accounts(or any other assets) you wanna access either programmatically or manually.

    Standard Apporach: Oauth2 flow.

    1. When users get onboarded to your system, ask them to sign in via FB and send the short lived token to Backend servers. OAuth2
    2. Exchange the short lived token for long lived one with FB. Long lived tokens have expiry of 60days.