脸书好友列表

发布于 2024-10-18 13:14:21 字数 162 浏览 1 评论 0原文

我如何使用新的 facebook api 获取我的好友列表。我使用下面的代码获取了好友列表,

$friends = $facebook->api('/uid/friends');

但如果我传递他们的用户 ID,我将无法获取好友列表一样的功能..

How can i get the list of my friends friends list using new facebook api. i got the friend list using the below code,

$friends = $facebook->api('/uid/friends');

but i am not able to get friend list if i pass their userid in the same function..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

柳若烟 2024-10-25 13:14:21

你不能,那是他们的私人数据,所以它在他们的控制之下。如果他们希望应用程序能够访问它,他们会授予许可,并且应用程序可以代表他们这样做。

You can't, that's their private data so it's under their control. If they then want the application to have access to it, they would grant permission and it can do so on their behalf.

流年里的时光 2024-10-25 13:14:21

Facebook 好友列表仅获取可标记的好友列表。

Facebook 不会访问完整的个人资料好友列表,它仅显示您个人资料中的好友列表总数。

如果您应用 Facebook 开发者步骤,pod 和 plist 设置所有其他内容。

这是我在 Swift 3.0 可标记好友列表中的代码。

appdelegate.swift

import CoreData
import FBSDKCoreKit
import FBSDKLoginKit
  func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
         return FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)
        // Override point for customization after application launch.
        return true
    }
    
    func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
        return FBSDKApplicationDelegate.sharedInstance().application(app, open: url, options: options)
    }

viewcontroller.swift

import UIKit
import FBSDKLoginKit
import FacebookCore
import FacebookLogin
import SDWebImage


class ViewController: UIViewController, FBSDKLoginButtonDelegate,UITableViewDelegate,UITableViewDataSource

{
    @IBOutlet var TblView: UITableView!
    @IBOutlet weak var btnFacebook: FBSDKLoginButton!

    @IBOutlet weak var ivUserProfileImage: UIImageView!

    @IBOutlet weak var lblName: UILabel!
    
     let reusableIdentifire = "cell"
    @IBOutlet var FriendListBtn: UIButton!
    
    var dict  = [AnyObject]()
    var FBNameArray = [String]()
    var FBImageArray = [String]()
    var Array = [String:AnyObject]()
    
    override func viewDidLoad()
    {
        super.viewDidLoad()
        self.TblView.delegate = self
        self.TblView.dataSource = self
        
      configureFacebook()
        
        //acess authorization 
        let fbLoginManager : FBSDKLoginManager = FBSDKLoginManager()
        fbLoginManager.logIn(withReadPermissions: ["email"], from: self) { (result, error) in
            if (error == nil){
                let fbloginresult : FBSDKLoginManagerLoginResult = result!
                if fbloginresult.grantedPermissions != nil {
                    if(fbloginresult.grantedPermissions.contains("email"))
                    {
                        self.getFBUserData()
                        fbLoginManager.logOut()
                    }
                }
            }
        }
            self.TblView.reloadData()
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        return FBNameArray.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
        let cell: TableViewCell = self.TblView.dequeueReusableCell(withIdentifier: self.reusableIdentifire,for:indexPath) as! TableViewCell
        
        cell.FriendName.text = self.FBNameArray[indexPath.row]
        cell.FriendImg?.sd_setImage(with:URL(string: FBImageArray[indexPath.row] ), placeholderImage: UIImage(named: ""))

        //cell.FriendImg.image = UIImage(named: self.FBImageArray[indexPath.row])
        
        
        return cell
        
    }

    
    func configureFacebook()
    {
        btnFacebook.readPermissions = ["public_profile", "email", "user_friends"];
        btnFacebook.delegate = self
    }
    
    
    
    func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError!)
    {}
    
    func loginButtonDidLogOut(_ loginButton: FBSDKLoginButton!)
    {}
    
    func loginButton(_ loginButton: FBSDKLoginButton!, didCompleteWith result: FBSDKLoginManagerLoginResult!, error: Error!)
    {
        FBSDKGraphRequest.init(graphPath: "me", parameters: ["fields":"first_name, last_name, picture.type(large)"]).start { (connection, result, error) -> Void in
                       
            let strFirstName: String = ((result as AnyObject)["first_name"]) as! String
            let strLastName: String = ((result as AnyObject)["last_name"]) as! String
            let faceBookID:String = (result as AnyObject).value(forKey: "id") as! String
            let strPictureURL = String(format:"https://graph.facebook.com/%@/picture?type=large", faceBookID)
            
            self.lblName.text = "Welcome, \(strFirstName) \(strLastName)"
            self.ivUserProfileImage.image = UIImage(data: try! Data(contentsOf: URL(string: strPictureURL)!))
        }
    }
    private func loginButtonDidLogOut(loginButton: FBSDKLoginButton!)
    {
        let loginManager: FBSDKLoginManager = FBSDKLoginManager()
        ivUserProfileImage.image = nil
        lblName.text = "SignOut"

        loginManager.logOut()
        
           }
    
    func getFBUserData(){
        
        if((FBSDKAccessToken.current()) != nil){

            FBSDKGraphRequest(graphPath:"/me/taggable_friends", parameters:["fields":"user_id,first_name,last_name,name,picture.type(large)"],httpMethod: "GET")
                //["fields": "id, name, first_name, last_name, picture.type(large), email"])
                .start(completionHandler: { (connection,result, error) -> Void in
                if (error == nil)
                {
                    print("Friends are: \(result)")
                    let jsonResult = result as! Dictionary<String,AnyObject>
                    self.dict = jsonResult["data"] as! [AnyObject]
                    
                    for item in self.dict
                    {
                       // let id = item["id"] as! String
                        let name = item["name"] as! String
                        self.FBNameArray.append(name)
                        print("FBname: \(self.FBNameArray)")
                        let picture = item["picture"] as! NSDictionary
                        let parsePic = picture["data"] as! NSDictionary
                        let url = parsePic["url"] as! String
                        self.FBImageArray.append(url)
                        print("FBimage: \(self.FBImageArray)")
                        self.TblView.reloadData()
                    }
                    
                }
                    
            })
        }
    }

}

tableview.swift

import UIKit
import FBSDKLoginKit
import FacebookCore
import FacebookLogin

class TableViewController: UIViewController,UITableViewDelegate,UITableViewDataSource
{

    @IBOutlet var TblView: UITableView!
    
    let reusableIdentifire = "cell"
    
    var ImageArray = [String]()
    var NameArray = [String]()

    override func viewDidLoad()
    {
        super.viewDidLoad()
        
        self.TblView.delegate = self
        self.TblView.dataSource = self
        
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        return 1
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
        let cell: TableViewCell = self.TblView.dequeueReusableCell(withIdentifier: self.reusableIdentifire,for:indexPath) as! TableViewCell
        
            cell.FriendName.text = self.NameArray[indexPath.row]
            cell.FriendImg.image = UIImage(named: self.ImageArray[indexPath.row])
        return cell
    }
    
    @IBAction func BackBtn(_ sender: AnyObject)
    {
        self.navigationController?.popViewController(animated:true)
    }

}

tableviewcell.swift

import UIKit

class TableViewCell: UITableViewCell
{

    @IBOutlet var FriendImg: UIImageView!
    @IBOutlet var FriendName: UILabel!
}

Facebook friend list only fetches a taggable friend list.

Facebook does not access full profiles friend list, it shows only total friend list count in your profile.

pod and plist setting all other should you apply Facebook Developer steps.

So is here my code in Swift 3.0 taggable friend list.

appdelegate.swift

import CoreData
import FBSDKCoreKit
import FBSDKLoginKit
  func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
         return FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)
        // Override point for customization after application launch.
        return true
    }
    
    func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
        return FBSDKApplicationDelegate.sharedInstance().application(app, open: url, options: options)
    }

viewcontroller.swift

import UIKit
import FBSDKLoginKit
import FacebookCore
import FacebookLogin
import SDWebImage


class ViewController: UIViewController, FBSDKLoginButtonDelegate,UITableViewDelegate,UITableViewDataSource

{
    @IBOutlet var TblView: UITableView!
    @IBOutlet weak var btnFacebook: FBSDKLoginButton!

    @IBOutlet weak var ivUserProfileImage: UIImageView!

    @IBOutlet weak var lblName: UILabel!
    
     let reusableIdentifire = "cell"
    @IBOutlet var FriendListBtn: UIButton!
    
    var dict  = [AnyObject]()
    var FBNameArray = [String]()
    var FBImageArray = [String]()
    var Array = [String:AnyObject]()
    
    override func viewDidLoad()
    {
        super.viewDidLoad()
        self.TblView.delegate = self
        self.TblView.dataSource = self
        
      configureFacebook()
        
        //acess authorization 
        let fbLoginManager : FBSDKLoginManager = FBSDKLoginManager()
        fbLoginManager.logIn(withReadPermissions: ["email"], from: self) { (result, error) in
            if (error == nil){
                let fbloginresult : FBSDKLoginManagerLoginResult = result!
                if fbloginresult.grantedPermissions != nil {
                    if(fbloginresult.grantedPermissions.contains("email"))
                    {
                        self.getFBUserData()
                        fbLoginManager.logOut()
                    }
                }
            }
        }
            self.TblView.reloadData()
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        return FBNameArray.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
        let cell: TableViewCell = self.TblView.dequeueReusableCell(withIdentifier: self.reusableIdentifire,for:indexPath) as! TableViewCell
        
        cell.FriendName.text = self.FBNameArray[indexPath.row]
        cell.FriendImg?.sd_setImage(with:URL(string: FBImageArray[indexPath.row] ), placeholderImage: UIImage(named: ""))

        //cell.FriendImg.image = UIImage(named: self.FBImageArray[indexPath.row])
        
        
        return cell
        
    }

    
    func configureFacebook()
    {
        btnFacebook.readPermissions = ["public_profile", "email", "user_friends"];
        btnFacebook.delegate = self
    }
    
    
    
    func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError!)
    {}
    
    func loginButtonDidLogOut(_ loginButton: FBSDKLoginButton!)
    {}
    
    func loginButton(_ loginButton: FBSDKLoginButton!, didCompleteWith result: FBSDKLoginManagerLoginResult!, error: Error!)
    {
        FBSDKGraphRequest.init(graphPath: "me", parameters: ["fields":"first_name, last_name, picture.type(large)"]).start { (connection, result, error) -> Void in
                       
            let strFirstName: String = ((result as AnyObject)["first_name"]) as! String
            let strLastName: String = ((result as AnyObject)["last_name"]) as! String
            let faceBookID:String = (result as AnyObject).value(forKey: "id") as! String
            let strPictureURL = String(format:"https://graph.facebook.com/%@/picture?type=large", faceBookID)
            
            self.lblName.text = "Welcome, \(strFirstName) \(strLastName)"
            self.ivUserProfileImage.image = UIImage(data: try! Data(contentsOf: URL(string: strPictureURL)!))
        }
    }
    private func loginButtonDidLogOut(loginButton: FBSDKLoginButton!)
    {
        let loginManager: FBSDKLoginManager = FBSDKLoginManager()
        ivUserProfileImage.image = nil
        lblName.text = "SignOut"

        loginManager.logOut()
        
           }
    
    func getFBUserData(){
        
        if((FBSDKAccessToken.current()) != nil){

            FBSDKGraphRequest(graphPath:"/me/taggable_friends", parameters:["fields":"user_id,first_name,last_name,name,picture.type(large)"],httpMethod: "GET")
                //["fields": "id, name, first_name, last_name, picture.type(large), email"])
                .start(completionHandler: { (connection,result, error) -> Void in
                if (error == nil)
                {
                    print("Friends are: \(result)")
                    let jsonResult = result as! Dictionary<String,AnyObject>
                    self.dict = jsonResult["data"] as! [AnyObject]
                    
                    for item in self.dict
                    {
                       // let id = item["id"] as! String
                        let name = item["name"] as! String
                        self.FBNameArray.append(name)
                        print("FBname: \(self.FBNameArray)")
                        let picture = item["picture"] as! NSDictionary
                        let parsePic = picture["data"] as! NSDictionary
                        let url = parsePic["url"] as! String
                        self.FBImageArray.append(url)
                        print("FBimage: \(self.FBImageArray)")
                        self.TblView.reloadData()
                    }
                    
                }
                    
            })
        }
    }

}

tableview.swift

import UIKit
import FBSDKLoginKit
import FacebookCore
import FacebookLogin

class TableViewController: UIViewController,UITableViewDelegate,UITableViewDataSource
{

    @IBOutlet var TblView: UITableView!
    
    let reusableIdentifire = "cell"
    
    var ImageArray = [String]()
    var NameArray = [String]()

    override func viewDidLoad()
    {
        super.viewDidLoad()
        
        self.TblView.delegate = self
        self.TblView.dataSource = self
        
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        return 1
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
        let cell: TableViewCell = self.TblView.dequeueReusableCell(withIdentifier: self.reusableIdentifire,for:indexPath) as! TableViewCell
        
            cell.FriendName.text = self.NameArray[indexPath.row]
            cell.FriendImg.image = UIImage(named: self.ImageArray[indexPath.row])
        return cell
    }
    
    @IBAction func BackBtn(_ sender: AnyObject)
    {
        self.navigationController?.popViewController(animated:true)
    }

}

tableviewcell.swift

import UIKit

class TableViewCell: UITableViewCell
{

    @IBOutlet var FriendImg: UIImageView!
    @IBOutlet var FriendName: UILabel!
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文