2016-02-15 76 views
-2

我怎樣才能獲得該組ID來自Facebook API我怎樣才能從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>"; 
} 

我不得不碼,其中我得到 陣列([名稱] => XXXX [FIRST_NAME] = > XXXX [姓氏] => XXXX [郵件] => XXXX [ID] => XXXXX)

現在我想組ID怎麼可能請協助

回答

0

訪問用戶的羣體,你需要使用User Groups endpoint

要閱讀你需要做的另一個API調用用戶的Facebook的羣體:

/{user-id}/groups 

既然你已經取回你的「/我」呼叫ID,你幾乎沒有。

可以稍微改變你的代碼如下:

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

如何讓你管理的文檔進行很好的解釋的羣體,包括示例代碼列表:https://developers.facebook.com/docs/graph-api/reference/user/groups/

您需要以授權與user_managed_groups permisssion並呼籲端點的/me/groups端點。不要使用用戶ID爲終點,只是用/me - 它指向當前用戶無論如何:

$groups_request = $fb->get('/me/groups'); 
$groups = $groups_request->getGraphNode()->asArray(); 

要知道,你只能得到你管理組,你不能到組的訪問只是一個成員。user_groups獲取所有羣組的權限已被刪除:https://developers.facebook.com/docs/apps/changelog#v2_4(搜索「90天過期」)