Facebook API not working for Posting on Wall

Hello amazing guys,

I’m having some problems integrating facebook into my game. Basically the login popup works, I can login, grant permissions to my app… and then… the popup disappears and it brings me back to my game… without posting anything at all on my wall…

Thanks in advance for the help!

This is my code:

[code]function facebookListener( event )

— End of debug Event routine -------------------------------------------------------

print( “event.name”, event.name ) – “fbconnect”
print( “event.type:”, event.type ) – type is either “session” or “request” or “dialog”
print( "isError: " … tostring( event.isError ) )
print( "didComplete: " … tostring( event.didComplete) )

– After a successful login event, send the FB command
– Note: If the app is already logged in, we will still get a “login” phase

if ( “session” == event.type ) then
– event.phase is one of: “login”, “loginFailed”, “loginCancelled”, “logout”
statusMessage.textObject.text = event.phase – tjn Added

print( "Session Status: " … event.phase )

if event.phase ~= “login” then
– Exit if login error
return
end

– The following displays a Facebook dialog box for posting to your Facebook Wall
if fbCommand == SHOW_DIALOG then
facebook.showDialog( {action=“stream.publish”} )
end

– This code posts a message to your Facebook Wall
if fbCommand == POST_MSG then
local time = os.date("*t")
local postMsg = {
message=“Hey I’m playing Flying Squirrel on my iPhone and I scored " … score … " points!”,
name=“Do you think you can beat my score?”,
caption=“Play Flying Squirrel now to find out!”,
link=“http://www.drowne.com”,
picture=“http://www.drowne.com/images/squirrelloading.png” }

facebook.request( “me/feed”, “POST”, postMsg ) – posting the message
end

elseif ( “request” == event.type ) then
– event.response is a JSON object from the FB server
local response = event.response

if ( not event.isError ) then
response = json.decode( event.response )

if fbCommand == POST_MSG then
print( response, “message”, 3 )
statusMessage.textObject.text = “Message Posted”

else
– Unknown command response
print( “Unknown command response” )
statusMessage.textObject.text = “Unknown ?”
end

else
– Post Failed
statusMessage.textObject.text = “Post failed”
printTable( event.response, “Post Failed Response”, 3 )
end

elseif ( “dialog” == event.type ) then
– showDialog response

print( “dialog response:”, event.response )
statusMessage.textObject.text = event.response
end

end

local statusMessage

function publishOnFacebook()
statusMessage = createStatusMessage( “Not connected”, 0.5*display.contentWidth, 30 )
fbCommand = POST_MSG
facebook.login( appId, facebookListener, {“publish_stream”} )
end[/code] [import]uid: 23177 topic_id: 13976 reply_id: 313976[/import]

Hey there,

Take a look in the CoronaSDK folder, then Sample Code, Networking, Facebook.

That’s a working example of how to post - you can copy it right into your app then modify it to suit your needs :slight_smile: [import]uid: 52491 topic_id: 13976 reply_id: 51548[/import]

That’s exactly what I did and it’s not working… Any ideas why, looking at the code? [import]uid: 23177 topic_id: 13976 reply_id: 51569[/import]

Ok, I fixed it… It was and elseif somewhere in the wrong position, this is the working code:

[code]local function createStatusMessage( message, x, y )
– Show text, using default bold font of device (Helvetica on iPhone)
local textObject = display.newText( message, 0, 0, native.systemFontBold, 24 )
textObject:setTextColor( 255,255,255 )

– A trick to get text to be centered
local group = display.newGroup()
group.x = x
group.y = y
group:insert( textObject, true )

– Insert rounded rect behind textObject
local r = 10
local roundedRect = display.newRoundedRect( 0, 0, textObject.contentWidth + 2*r, textObject.contentHeight + 2*r, r )
roundedRect:setFillColor( 55, 55, 55, 190 )
group:insert( 1, roundedRect, true )

group.textObject = textObject
return group
end

local function getInfo_onRelease( event )
– call the login method of the FB session object, passing in a handler
– to be called upon successful login.
fbCommand = GET_USER_INFO
facebook.login( appId, facebookListener, {“publish_stream”} )
end

local function postMsg_onRelease( event )
– call the login method of the FB session object, passing in a handler
– to be called upon successful login.
fbCommand = POST_MSG
facebook.login( appId, facebookListener, {“publish_stream”} )
end

local function showDialog_onRelease( event )
– call the login method of the FB session object, passing in a handler
– to be called upon successful login.
fbCommand = SHOW_DIALOG
facebook.login( appId, facebookListener, {“publish_stream”} )
end

local function logOut_onRelease( event )
– call the login method of the FB session object, passing in a handler
– to be called upon successful login.
fbCommand = LOGOUT
facebook.logout()
end

function removeStatusMessage()
statusMessage.isVisible = false
statusMessage:removeSelf()
end

local function facebookListener( event )
print( “Facebook Listener events:” )

local maxStr = 20 – set maximum string length
local endStr

for k,v in pairs( event ) do
local valueString = tostring(v)
if string.len(valueString) > maxStr then
endStr = " … #" … tostring(string.len(valueString)) … “)”
else
endStr = “)”
end
print( " " … tostring( k ) … “(” … tostring( string.sub(valueString, 1, maxStr ) ) … endStr )
end
— End of debug Event routine -------------------------------------------------------

print( “event.name”, event.name ) – “fbconnect”
print( “event.type:”, event.type ) – type is either “session” or “request” or “dialog”
print( "isError: " … tostring( event.isError ) )
print( "didComplete: " … tostring( event.didComplete) )

– After a successful login event, send the FB command
– Note: If the app is already logged in, we will still get a “login” phase

if ( “session” == event.type ) then
– event.phase is one of: “login”, “loginFailed”, “loginCancelled”, “logout”
statusMessage.textObject.text = event.phase – tjn Added

print( "Session Status: " … event.phase )

if event.phase ~= “login” then
statusMessage.textObject.text = “login error”
– Exit if login error
return
end

– The following displays a Facebook dialog box for posting to your Facebook Wall
if fbCommand == SHOW_DIALOG then
statusMessage.textObject.text = “show dialog”
facebook.showDialog( {action=“stream.publish”} )
end

– Request the Platform information (FB information)
if fbCommand == GET_PLATFORM_INFO then
statusMessage.textObject.text = “platform info”
facebook.request( “platform” ) – **tjn Displays info about Facebook platform
end

– Request the current logged in user’s info
if fbCommand == GET_USER_INFO then
statusMessage.textObject.text = “get user info”
facebook.request( “me” )
– facebook.request( “me/friends” ) – Alternate request
end

– This code posts a photo image to your Facebook Wall

if fbCommand == POST_PHOTO then

statusMessage.textObject.text = “post photo”

local attachment = {
name = “Developing a Facebook Connect app using the Corona SDK!”,
link = “http://developer.anscamobile.com/forum”,
caption = “Link caption”,
description = “Corona SDK for developing iOS and Android apps with the same code base.”,
picture = “http://developer.anscamobile.com/demo/Corona90x90.png”,
actions = json.encode( { { name = “Learn More”, link = “http://anscamobile.com” } } )
}

facebook.request( “me/feed”, “POST”, attachment ) – posting the photo
end

– This code posts a message to your Facebook Wall
if fbCommand == POST_MSG then

statusMessage.textObject.text = “posting message”

local postMsg = {
message = “I’m playing Flying Squirrel from my iPhone and I scored " … globalScore … " points!!! Try to beat me!”
}

facebook.request( “me/feed”, “POST”, postMsg ) – posting the message
end

end

if ( “request” == event.type ) then
– event.response is a JSON object from the FB server
local response = event.response
statusMessage.textObject.text = “response”
timer.performWithDelay(1000, removeStatusMessage)

if ( not event.isError ) then
response = json.decode( event.response )

statusMessage.textObject.text = “response not error”

if fbCommand == GET_USER_INFO then
statusMessage.textObject.text = response.name
print( “name”, response.name )

elseif fbCommand == POST_PHOTO then
statusMessage.textObject.text = “Photo Posted”

elseif fbCommand == POST_MSG then
statusMessage.textObject.text = “Message Posted”
else
– Unknown command response
print( “Unknown command response” )
statusMessage.textObject.text = “Post failed, please try again later!”
end

else
– Post Failed
statusMessage.textObject.text = “Post failed, please try again later!”
end

elseif ( “dialog” == event.type ) then
– showDialog response

print( “dialog response:”, event.response )
statusMessage.textObject.text = “event.response”
end
end

function publishOnFacebook()
statusMessage = createStatusMessage( “Connecting to Facebook”, 0.5*display.contentWidth, 30 )
fbCommand = POST_MSG
facebook.login( appId, facebookListener, {“publish_stream”} )
end[/code] [import]uid: 23177 topic_id: 13976 reply_id: 51580[/import]