首页 文章

通过Facebook Graph API访问用户的朋友?

提问于
浏览
6

有没有人通过Facebook Graph API成功访问用户朋友列表?

我编写了代码,通过Facebook的OAuth API对我网站上的用户进行身份验证 . 它的作品非常出色;我可以访问公共信息,喜欢,兴趣,活动等 . 我通过以下URL格式访问这些数据点,我用“uid”替换有效的id(我使用uid代替“me”,因为“我”不像宣传的那样工作):

https://graph.facebook.com/uid?access_token= ...
https://graph.facebook.com/uid/likes?access_token= ...
https://graph.facebook.com/uid/interests?access_token= ...
https://graph.facebook.com/uid/activities?access_token= ...

但是,访问朋友根本不起作用 . 网址,

https://graph.facebook.com/uid/friends?access_token= ...

返回以下错误:

{
   "error": {
      "type": "OAuthAccessTokenException",
      "message": "An access token is required to request this resource."
   }
}

Graph API docs上的示例都有效,但在该页面上生成的访问令牌的格式与我的格式不同 . 我按照他们的指示去了一个发球台(让它为其他一切工作!) .

朋友被认为是publicly accessible information,我认为这是问题所在 .

4 回答

  • 0

    如果用户连接到您的应用程序,您可以检索他们的朋友,如下所示:

    $facebook = new Facebook(array(
                    'appId' => 'xxxxxxxx',
                    'secret' => 'xxxxxxx',
                    'cookie' => true,
                ));
    
        // $session is only != null, when you have the session-cookie, that is set by facebook, after the user logs in
        $session = $facebook->getSession(); 
    
        // you dont get a list of friends, but a list, which contains other friendlists
        $friendsLists = $facebook->api('/me/friends');
    
        foreach ($friendsLists as $friends) {
          foreach ($friends as $friend) {
             // do something with the friend, but you only have id and name
             $id = $friend['id'];
             $name = $friend['name'];
          }
       }
    

    但您只能访问朋友的ID和姓名 .

  • 0

    你在哪里可以公开访问这些朋友?您链接到州的常见问题解答:

    公开信息包括您的姓名, Profiles 图片,性别和网络 . 此信息使您认识的朋友,家人和其他人更容易与您联系 .

    我认为该段中没有提到朋友 .

  • 7

    您需要先获取访问令牌 . 看到:

    Facebook Graph API — getting access tokens

  • 0

    您看到的错误暗示 access_token 未被发送 . 这也可能是 /me 适合您的原因 . 一旦你解决了这个问题,朋友的联系也会像你期望的那样出现 .

相关问题