Reputation: 534
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!
Upvotes: 1
Views: 1848
Reputation: 552
swift 5 update
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)")
}
})
}
Upvotes: 1
Reputation: 534
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)")
})
}
Upvotes: 0