Search code examples
phpfacebook-graph-apifacebook-marketing-api

Graph returned an error requires extended permission business_management


My app has ads_management, business_management and standard level permissions so When I try to login in from my Facebook account then it retrieves information successfully. But when my customer login with fb then it shows error

Graph returned an error: (#200) Requires extended permission: business_management

I am making following API call v2.8

$fb = new \Facebook\Facebook([
 'app_id'                 => $appId,
 'app_secret'         => $appSecret,
 'default_graph_version' => $graph_api_version,
]);

$response = $fb->get('/me/businesses', $accessToken); 

enter image description here

enter image description here

What can be the reasons and possible solutions?


Solution

  • Try this

    $fb = new \Facebook\Facebook([
     'app_id'                 => $appId,
     'app_secret'         => $appSecret,
     'default_graph_version' => $graph_api_version,
    ]);
    
    $helper = $fb->getRedirectLoginHelper();
    
    $permissions = [
        'email', 
        'user_friends', 
        'manage_pages', 
        'business_management',
        'ads_management'
    ];
    
    
    $fb_url = $helper->getLoginUrl(
        <callbackUrl>, 
        $permissions
    );
    

    Redirect use to this url. Facebook will redirect the user again back to the callback url after taking permissions from the user. Once customer comes back to portal via this url, you will get the access token having permissions of the extended permission which you exchange for long-lived access token by short-lived access token and query the requried graph-api

    Callback Script

    $fb = new \Facebook\Facebook([
        'app_id'                 => $appId,
        'app_secret'         => $appSecret,
        'default_graph_version' => $graph_api_version,
    ]);
    
    $helper = $fb->getRedirectLoginHelper();
    
    try {
        $accessToken = $helper->getAccessToken();
    } catch (Facebook\Exceptions\FacebookResponseException $e) {
        // When Graph returns an error
        echo 'Graph returned an error: ' . $e->getMessage();
        exit;
    } catch (Facebook\Exceptions\FacebookSDKException $e) {
        // When validation fails or other local issues
        echo 'Facebook SDK returned an error: ' . $e->getMessage();
        exit;
    }
    
    // The OAuth 2.0 client handler helps us manage access tokens
    $oAuth2Client = $fb->getOAuth2Client();
    
    // Exchange short-lived access token with long-lived access token
    if (!$accessToken->isLongLived()) {
        // Exchanges a short-lived access token for a long-lived one
        try {
            $accessToken = $oAuth2Client->getLongLivedAccessToken($accessToken);
        } catch (Facebook\Exceptions\FacebookSDKException $e) {
            echo "<p>Error getting long-lived access token: " . $helper->getMessage() . "</p>\n\n";
            exit;
        }
    }
    

    Store access token and redirect user where you query this

    $response = $fb->get('/me/businesses', $accessToken);