图表返回错误需要扩展权限business_management

时间:2016-11-01 10:06:37

标签: php facebook-graph-api facebook-marketing-api

我的应用具有ads_management,business_management和标准级权限,因此当我尝试从我的Facebook帐户登录时,它会成功检索信息。但是当我的客户使用fb登录时,它会显示错误

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

我正在进行以下API调用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

可能是什么原因和可能的解决方案?

1 个答案:

答案 0 :(得分:1)

试试这个

$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
);

将使用重定向到此网址。在从用户获取权限后,Facebook将再次将用户重定向回回调网址。 一旦客户通过此URL回到门户网站,您将获得具有扩展权限权限的访问令牌,您可以通过短期访问令牌交换长期访问令牌并查询所需的图表-api

回调脚本

$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;
    }
}

存储访问令牌并将用户重定向到您查询的位置

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