简体   繁体   English

使用Swift从iOS应用程序发布到Facebook

[英]Post to Facebook from iOS app with Swift

So I have been investigating this for quite some time now and have decided to reach out and ask the masses. 所以我现在已经调查了很长一段时间,并决定联系并向群众询问。

I know that when posting to Facebook through an iOS app, the app needs to do a read before a write, ie Read users email, before posting to a wall. 我知道,当通过iOS应用程序发布到Facebook时,应用程序需要在写入之前进行读取,即读取用户电子邮件,然后发布到墙上。 And I know that these permission lists cannot be done in the same request. 我知道这些权限列表不能在同一个请求中完成。 So I have pieced together code that looks as following. 所以我拼凑了看起来如下的代码。

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

Unfortunately I am still getting 不幸的是我还在

The Facebook server could not fulfill this access request: Invalid permission: publish_stream

I have setup my app on the Facebook dev site and I have also added the correct bundle identifier to the dev site. 我已经在Facebook开发站点上设置了我的应用程序,并且我还在开发站点添加了正确的包标识符。

好的,所以答案是“publish_stream”似乎不是作为权限工作,尽管许多教程使用它。

Have you tried adding your FB app id to the plist? 您是否尝试将FB应用程序ID添加到plist?

<key>FacebookAppID</key>
<string>101010101010</string>
<key>FacebookDisplayName</key>
<string>MyCoolProject</string>

You get the FB app id from the developer site. 您从开发者网站获得FB应用程序ID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM