4

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, i.e. Read users email, before posting to a wall. 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.

richari1987
  • 360
  • 2
  • 19
  • Eh whats with the down vote? This is a genuine question? Something I'm missing here?!?!? Ill remove the question if its a dupe or obvious, but a little hint in the right direction would be much appreciated – richari1987 Aug 22 '14 at 22:32

2 Answers2

3

OK so the answer was that "publish_stream" does not seem to work as a permission despite many tutorials that use it.

richari1987
  • 360
  • 2
  • 19
0

Have you tried adding your FB app id to the plist?

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

You get the FB app id from the developer site.

LMVogel
  • 779
  • 7
  • 28