我想将我的应用发布到我的Facebook商家页面。我可以将它发布到我的Facebook Facebook Feed中,我也可以将其发布到我的商家页面,作为我的个人用户帐户发布。但是,我希望将其作为商家帐户发布到主要商家页面Feed。
如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)")
})
}
感谢您提供的任何建议提示!
答案 0 :(得分:1)
迅速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)")
}
})
}
答案 1 :(得分:0)
我最终得到了自己的工作。对于那些试图做同样事情的人,我会分享答案。 要将简单的短信发布到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)")
})
}