使用 Swift 作为 Facebook 页面发布

Posted

技术标签:

【中文标题】使用 Swift 作为 Facebook 页面发布【英文标题】:Posting as a Facebook Page using Swift 【发布时间】:2017-11-03 21:57:35 【问题描述】:

我希望我的应用发布到我的 Facebook 业务页面。我可以让它发布到我的个人 Facebook 订阅源,我也可以让它发布到我的业务页面,作为我的个人用户帐户发布。不过,我希望它作为企业帐户发布到主要的企业页面供稿。

如 Facebook 文档中所示: https://developers.facebook.com/docs/pages/publishing

这是我尝试过的。以下代码使用我的个人用户帐户发布到页面。

我试图用 pageAccessToken 替换 POST GraphRequest 中的 accessToken 参数...但无法正常工作。

var pageAccessToken : GraphResponse?

func getPageAccessToken() 

    let connection = GraphRequestConnection()

    connection.add(GraphRequest(graphpath: "/\(pageID)?fields=access_token"))  httpResponse, result in
        switch result 
        case .success(let response):
            print("Graph Request Success: \(response)")
            self.pageAccessToken = response
        case .failed(let error):
            print("Graph Request Fail: \(error)")
        
    
    connection.start()



func requestPublishPermissions() 

    let loginManager = LoginManager()

    loginManager.logIn(publishPermissions: [ .managePages, .publishPages], viewController: self)  loginResult in
        switch loginResult 
        case .failed(let error):
            print(error)
        case .cancelled:
            print("User cancelled login.")
        case .success(let grantedPermissions, let declinedPermissions, let accessToken):
            print("grantedPermissions = \(grantedPermissions) \n" +
                "declinedPermissions = \(declinedPermissions) \n" +
                "accessToken = \(accessToken)")
        
    


func postMessage() 

    let requestPage : GraphRequest = GraphRequest(graphPath: "\(pageID)/feed", parameters: ["message" : "Hello Page!"], accessToken: AccessToken.current, httpMethod: .POST, apiVersion: .defaultVersion)

    requestPage.start( (connection, result) -> Void in
        print("RESULT = \(result)")
    )

感谢您提供的任何建议!

【问题讨论】:

【参考方案1】:

swift 5更新

let pageID = "yourFBPage"
var pageAccessToken : String?



func getPageAccessToken() 

    let connection = GraphRequestConnection()
    connection.add(GraphRequest(graphPath: "\(pageID)", parameters: ["fields": "access_token"]))  httpResponse, result,error  in
        if let error = error 
            print(error.localizedDescription)
           else 
            let info = result as! [String : AnyObject]
             if info["access_token"] as? String != nil 
                self.pageAccessToken = info["access_token"] as? String
             
            //print(result)
            self.postMessage()
          
    
    connection.start()


func postMessage() 
    let requestPage : GraphRequest = GraphRequest(graphPath: "\(pageID)/feed", parameters: ["message" : "Hello Page!"], tokenString: self.pageAccessToken, version: nil , httpMethod: .post )
        

    requestPage.start(completionHandler:  (connection, result, error) -> Void in
        if let error = error 
            print(error.localizedDescription)
           else 
            //print("RESULT = \(result)")
          
    )

【讨论】:

【参考方案2】:

我最终让它自己工作了。对于那些试图做同样事情的人,我将分享答案。 向 Facebook 页面发布简单的短信:

let pageID = Get Your Page ID On Facebook
var pageAccessToken : AccessToken?

func requestPublishPermissions() 
    let loginManager = LoginManager()
    loginManager.logIn(publishPermissions: [ .managePages, .publishPages], viewController: self)  loginResult in
        switch loginResult 
        case .failed(let error):
            print(error)
        case .cancelled:
            print("User cancelled login.")
        case .success(let grantedPermissions, let declinedPermissions, let accessToken):
            print("grantedPermissions = \(grantedPermissions) \n" +
                "declinedPermissions = \(declinedPermissions) \n" +
                "accessToken = \(accessToken)")
        
    


func getPageAccessToken() 

    let connection = GraphRequestConnection()
    connection.add(GraphRequest(graphPath: "/\(pageID)?fields=access_token"))  httpResponse, result in
        switch result 
        case .success(let response):
            print("Graph Request Success: \(response)")
            self.pageAccessToken = AccessToken.init(authenticationToken: response.dictionaryValue?["access_token"] as! String)
        case .failed(let error):
            print("Graph Request Fail: \(error)")
        
    
    connection.start()


func postMessage() 
    let requestPage : GraphRequest = GraphRequest(graphPath: "\(pageID)/feed", parameters: ["message" : "Hello Page!"], accessToken: self.pageAccessToken, httpMethod: .POST, apiVersion: .defaultVersion)

    requestPage.start( (connection, result) -> Void in
        print("RESULT = \(result)")
    )

【讨论】:

以上是关于使用 Swift 作为 Facebook 页面发布的主要内容,如果未能解决你的问题,请参考以下文章

Facebook 使用 Swift 和 Parse 登录到主 ViewController

在 Swift 中使用 Facebook 异步显示工具包

Facebook : 如何使用 xmpp 将私人消息作为粉丝页面发送

在 iOS9 和 Swift2 中使用 Parse 登录 Facebook

PHP Facebook SDK 4.0 作为页面服务器发布到服务器

如何通过 iOS 中的图形 API 在 Facebook 页面上作为 Page Post 从应用发布?