所以我已经调查了很长时间,并决定联系群众。
我知道,当通过 iOS 应用程序发布到 Facebook 时,该应用程序需要先读后写,即阅读用户电子邮件,然后再发布到墙上。而且我知道这些权限列表不能在同一个请求中完成。所以我拼凑了如下所示的代码。
class func postToFacebook(message: String, appID: String){
var accountStore = ACAccountStore()
var accountType = accountStore.accountTypeWithAccountTypeIdentifier(ACAccountTypeIdentifierFacebook)
var optionsForPosting = [ACFacebookAppIdKey:appID, ACFacebookPermissionsKey: ["email"], ACFacebookAudienceKey: ACFacebookAudienceFriends]
accountStore.requestAccessToAccountsWithType(accountType, options: optionsForPosting) {
granted, error in
if granted {
var options = [ACFacebookAppIdKey:appID, ACFacebookPermissionsKey: ["publish_stream", "publish_actions"], ACFacebookAudienceKey: ACFacebookAudienceFriends]
accountStore.requestAccessToAccountsWithType(accountType, options: options) {
granted, error in
if granted {
var accountsArray = accountStore.accountsWithAccountType(accountType)
if accountsArray.count > 0 {
var facebookAccount = accountsArray[0] as ACAccount
var parameters = Dictionary<String, AnyObject>()
parameters["access_token"] = facebookAccount.credential.oauthToken
parameters["message"] = message
var feedURL = NSURL(string: "https://graph.facebook.com/me/feed")
let posts = SLRequest(forServiceType: SLServiceTypeFacebook, requestMethod: SLRequestMethod.POST, URL: feedURL, parameters: parameters)
let handler: SLRequestHandler = { (response, urlResponse, error) in
println(response)
println(urlResponse.statusCode)
}
posts.performRequestWithHandler(handler)
}
}
else{
println("Access denied")
println(error.localizedDescription)
}
}
}
else{
println("Access denied")
println(error.localizedDescription)
}
}
}
不幸的是我仍然得到
The Facebook server could not fulfill this access request: Invalid permission: publish_stream
我已经在 Facebook 开发站点上设置了我的应用程序,并且我还向开发站点添加了正确的包标识符。