如何从facebook API获取组ID
<?php
session_start();
require_once __DIR__ . '/src/Facebook/autoload.php';
$fb = new Facebook\Facebook([
'app_id' => 'APP_ID',
'app_secret' => 'APP_SECRET',
'default_graph_version' => 'v2.4',
]);
$helper = $fb->getCanvasHelper();
$permissions = ['email']; // optionnal
try {
if (isset($_SESSION['facebook_access_token'])) {
$accessToken = $_SESSION['facebook_access_token'];
} else {
$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;
}
if (isset($accessToken)) {
if (isset($_SESSION['facebook_access_token'])) {
$fb->setDefaultAccessToken($_SESSION['facebook_access_token']);
} else {
$_SESSION['facebook_access_token'] = (string) $accessToken;
// OAuth 2.0 client handler
$oAuth2Client = $fb->getOAuth2Client();
// Exchanges a short-lived access token for a long-lived one
$longLivedAccessToken = $oAuth2Client->getLongLivedAccessToken($_SESSION['facebook_access_token']);
$_SESSION['facebook_access_token'] = (string) $longLivedAccessToken;
$fb->setDefaultAccessToken($_SESSION['facebook_access_token']);
}
// validating the access token
try {
$request = $fb->get('/me');
} catch(Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
if ($e->getCode() == 190) {
unset($_SESSION['facebook_access_token']);
$helper = $fb->getRedirectLoginHelper();
$loginUrl = $helper->getLoginUrl('https://apps.facebook.com/APP_NAMESPACE/', $permissions);
echo "<script>window.top.location.href='".$loginUrl."'</script>";
exit;
}
} catch(Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
// getting basic info about user
try {
$profile_request = $fb->get('/me?fields=name,first_name,last_name,email');
$profile = $profile_request->getGraphNode()->asArray();
} catch(Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
echo 'Graph returned an error: ' . $e->getMessage();
unset($_SESSION['facebook_access_token']);
echo "<script>window.top.location.href='https://apps.facebook.com/APP_NAMESPACE/'</script>";
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
// priting basic info about user on the screen
print_r($profile);
// Now you can redirect to another page and use the access token from $_SESSION['facebook_access_token']
} else {
$helper = $fb->getRedirectLoginHelper();
$loginUrl = $helper->getLoginUrl('https://apps.facebook.com/APP_NAMESPACE/', $permissions);
echo "<script>window.top.location.href='".$loginUrl."'</script>";
}
我有代码,我得到了 数组([name] =&gt; xxxx [first_name] =&gt; xxxx [last_name] =&gt; xxxx [email] =&gt; xxxx [id] =&gt; xxxxx)
现在我想要群组ID,如何可能请协助
答案 0 :(得分:3)
如何获取您管理的所有群组的列表在文档中得到了很好的解释,包括示例代码:https://developers.facebook.com/docs/graph-api/reference/user/groups/
您需要使用or
授权进行授权,并调用 user_managed_groups
端点。不要使用该端点的用户ID,只需使用/me/groups
- 它无论如何都指向当前用户:
/me
请注意,您只能获得自己管理的群组,而无法访问您所属的群组。取消所有群组的$groups_request = $fb->get('/me/groups');
$groups = $groups_request->getGraphNode()->asArray();
权限已被删除: https://developers.facebook.com/docs/apps/changelog#v2_4(搜索&#34; 90天弃用&#34;)
答案 1 :(得分:0)
要访问用户组,您需要使用User Groups endpoint
要阅读用户的Facebook群组,您需要通过以下方式进行另一次API调用:
$profile_request = $fb->get('/me?fields=name,first_name,last_name,email');
$profile = $profile_request->getGraphNode()->asArray();
$groups_request = $fb->get("{$profile['id']}/groups");
$groups = $groups_request->getGraphNode()->asArray();
由于您在“/ me”电话中检索了ID,因此您几乎就在那里。
您可以稍微更改一下代码:
{{1}}