0

Ive searched high and low for an answer to this on here but havent been able to find anything. Please answer if you can, its appreciated.

The Setup:

Im learning how to work with the PHP Facebook API SDK by using one of the examples as a base. Im new to working with APIs so Im a little confused and have been having a problem trying to list a users groups.

The Question:

Ive got the app made. Ive got the proper permissions and extended permissions. I am able to get the users name and basic info.

我认为我的问题在于访问获取用户组所需的扩展权限所需的访问令牌。我不确定我是否正确地将其发送到 API。

这就是我得到的:

<?
    if($user_id) {

      // We have a user ID, so probably a logged in user.
      // If not, we'll get an exception, which we handle below.
      try {
        $access_token = $facebook->getAccessToken();
        $user_profile = $facebook->api('/me','GET');
        $user_groups = $facebook->api('/me/groups','GET', array( 'access_token=' => $access_token ));

        echo "Name: " . $user_profile['name'];
        echo "<br />";
        echo "Groups: " . $user_groups['name'];

      } catch(FacebookApiException $e) {
        // If the user is logged out, you can have a 
        // user ID even though the access token is invalid.
        // In this case, we'll get an exception, so we'll
        // just ask the user to login again here.
        $args['scope'] = 'user_groups,user_likes';
        $login_url = $facebook->getLoginUrl($args);
        echo 'Please <a href="' . $login_url . '">login.</a>';
        error_log($e->getType());
        error_log($e->getMessage());
      }   
    } else {

      // No user, print a link for the user to login
      $args['scope'] = 'user_likes,user_groups';
      $login_url = $facebook->getLoginUrl($args);
      echo 'Please <a href="' . $login_url . '">login.</a>';

    }

  ?>

任何帮助表示赞赏。

谢谢。

4

1 回答 1

0

我不太确定,但这里有几点:

  1. 使用 facebook sdk 时没有理由手动添加访问令牌,这些将为您添加访问令牌。应该:

    $user_groups = $facebook->api('/me/groups','GET');

  2. "/me/groups"的 json 编码结果的格式与"/me "的格式完全不同。“/me”的结果只是一组键/值数据,“/me/groups”的结果被包装在一个“data”参数中,该参数是一个对象数组。正如用户对象文档中所述(在连接下):

    包含版本(old-0 或新 Group-1)、名称、id、管理员(如果用户是组的管理员)和书签顺序(在主页上的组书签列表中的什么位置,组为用户显示)。

  3. 如果对 facebook 如何响应不同的查询有疑问,请使用Graph Explorer 工具来测试不同的查询并查看 facebook 返回的响应。例如/me结果和 /me/groups的结果

于 2012-05-09T08:55:56.510 回答