Justin Lewis Justin Lewis - 6 months ago 51
iOS Question

Posting as a Facebook Page using Swift

I would like my app to post to my Facebook business page. I've can get it to post to my person Facebook feed, and I've also gotten it to post to my business page, posting as my personal user account. However, I'd like it to post to the main business page feed, as the business account.

As shown here in Facebook's documentation:
https://developers.facebook.com/docs/pages/publishing

Here's is what I've tried. The following code posts to the page using my personal user account.

I was trying to replace the accessToken parameter in the POST GraphRequest with the pageAccessToken... but couldn't get it to work.

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)")
})
}


Thank you for any advice tips you can give!

Answer Source

I eventually got it working on my own. I'll share the answer, for those trying to do the same thing. To post a simple text message to a page on 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)")
    })
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download