【问题标题】:How can I get the list of user_friends from a Facebook authorized user?如何从 Facebook 授权用户那里获取 user_friends 列表?
【发布时间】:2015-06-09 12:59:04
【问题描述】:

我正在研究用户授权应用程序查看他的user_friends后查看某个用户的朋友列表的可能性,但我不太了解这个想法,所以我想知道,有没有直接的方法无需通过图形 API 和其他东西即可查看列表? 我正在使用 Laravel 的 Socialite 包登录,它似乎工作得很好,并返回默认信息(电子邮件、姓名、头像等)以及其他允许的信息,如出生日期、位置和家乡。但是,我发现很难查看照片、帖子、朋友和群组等列表。即使用户允许我这样做。

提前非常感谢!

【问题讨论】:

    标签: php facebook laravel facebook-friends laravel-socialite


    【解决方案1】:

    你就是做不到。

    Facebook 不允许您获取好友列表,而只能获取已在使用您的应用的好友列表。在 API 的官方文档中,我们可以阅读:https://developers.facebook.com/docs/graph-api/reference/v2.3/user/friends

    这只会返回使用(通过 Facebook 登录)提出请求的应用的任何朋友。如果此人的朋友拒绝 user_friends 权限,该朋友将不会出现在此人的朋友列表中。

    更重要的是,你无法从 Laravel 获取使用 Socialite 的朋友列表,所以我建议你使用另一个 Laravel 包,比如这个:https://github.com/SammyK/LaravelFacebookSdk

    干杯!

    【讨论】:

    • 确实如此。 Laravel 社交名流仅用于身份验证。我切换到 SammyK 插件。谢谢。
    • 没错,除了Socialite部分,你绝对可以从Socialite获取好友列表!请参阅下面的帖子
    【解决方案2】:

    上述所有信息仍然正确,除了您可以通过将社交名流添加到控制器中的“字段和范围...”来获取朋友列表和更多信息...

    例如:

    $socialUser = Socialite::driver('facebook')->fields(['id', 'email', 'cover', 'name', 'first_name', 'last_name', 'age_range', 'link', 'gender', 'locale', 'picture', 'timezone', 'updated_time', 'verified', 'birthday', 'friends', 'relationship_status', 'significant_other','context','taggable_friends'])->scopes(['email','user_birthday','user_friends','user_relationships','user_relationship_details'])->user();
    

    【讨论】:

      【解决方案3】:

      这个答案只是扩展并解释了如何使用@jayenne 所说的来完成这项工作。

      在你的 redirect() 函数中,你会像这样调用:

          public function redirect() {
              return Socialite::driver('facebook')
                  ->fields([
                      'friends'
                  ])
                  ->scopes([
                      'user_friends'
                  ])->redirect();
          }
      

      在上面,我们要求社交名流请求user_friends 权限并将其存储到名为friends 的字段中。现在,在您的回调中,您可以像这样在 SocialiteUser 中获取 friends 字段:

         public function callback(SocialFacebookAccountService $service) {
              $socialiteUser = Socialite::driver('facebook')
                  ->fields(['friends'])
                  ->user();
      
              //Loop through all the facebook friends returned (who are already on your site and friends with the recently registered user)
              foreach($socialiteUser->user['friends']['data'] as $fbFriend) {
                  //do something with this facebook friend here
              }
              //Most tutorials log the user in like this; you define the $service and the method
              //You can find a tutorial in the Socialite docs/github repo
              $user = $service->createOrGetUser($socialiteUser);
              auth()->login($user);
              return redirect('/home');
         }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-10-07
        • 2011-05-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-03-13
        • 2013-11-23
        • 1970-01-01
        相关资源
        最近更新 更多