Facebook 1st time login success does not execute Wall Post even if it's coded to do so, it works the second time.

Hi :slight_smile: So I’m trying to get my app to post a status on Facebook. My code does post a status but only the second time I tap on the button. The first time the button is clicked, the user is redirected to the Facebook permission page, then because I’m asking for the ‘publish_actions’ permission, another Facebook permission page appears. When I tap the button to allow the app to post status, the Facebook permission page closes and I’m back to my app. The problem is that, even if I coded it to post status immediately, it doesnt do so, it does post the status if I click on the button again. If I click the button the 2nd time, the Facebook permission page pops out again every single time but is just black screen and quickly fades away.  Is this a natural behavior? Does this black screen always appear every time I post a status even if I’m already logged in? If this can be solved, how do I get rid of the black screen to appear? And why doesn’t my app post the status after the 1st login? I’ve been working on this problem for 1 week and still to no avail. One more thing, if my app logs in to Facebook and the app exits. If the user plays the app again, do I have to run the facebook login code again? This is my code so far… I’ve also attached screenshot of the app activity (Black screen appears everytime I post status). Thank you.

local function listener( event ) print( "event.name", event.name ) --"fbconnect" print( "event.type:", event.type ) --type is either "session", "request", or "dialog" print( "isError: " .. tostring( event.isError ) ) print( "didComplete: " .. tostring( event.didComplete ) ) --"session" events cover various login/logout events --"request" events handle calls to various Graph API calls --"dialog" events are standard popup boxes that can be displayed if ( "session" == event.type ) then --options are: "login", "loginFailed", "loginCancelled", or "logout" if ( "login" == event.phase ) then local access\_token = event.token facebook.request( "me/feed", "POST", { message="I scored ".. myData.score.." in FreakOMath. Try and beat my score!" } ) end elseif ( "request" == event.type ) then print("facebook request") if ( not event.isError ) then local response = json.decode( event.response ) end elseif ( "dialog" == event.type ) then print( "dialog", event.response ) end end local function buttonTouch(event) --take in the event and get the associated display object local myButtons = event.target --now find out which button it is local nameString = myButtons.id --use began and ended phases to change text based on touches if event.phase == "began" then --set the label text to the id elseif event.phase == "ended" then if nameString == "button0" then myData.score = 0 ads.hide() composer.gotoScene( "scene2", "slideLeft", 250 ) elseif nameString == "button1" then --replace with your Facebook App ID facebook.login( fbAppID, listener, {"publish\_actions"}) end end return true end

You should do two separate logins.  The first one asks for basic permissions.  Then when they press the button, it would login a second time asking for the extended permissions.  This is a Facebook requirement.  You can look at the Corona Labs Facebook sample code in SampleCode/Networking/Facebook/.

Then beyond that you should look at your device’s console log and look for any warning or errors.  If you need help with that, see:

http://coronalabs.com/blog/2013/07/09/tutorial-basic-debugging/

Make sure to run the “adb logcat” command with no other parameters that would filter just Corona messages.  You need to see the Facebook messages as well.

Rob

Hello Sir. Thank you for your reply. The first time I log in with this code…

facebook.login( fbAppID, listener, {“publish_actions”})

It gets me to a 2 process login like you said, the 1st one for basic permissions and the 2nd one for ‘publish_actions’. On succeeding button taps after the 1st one, a black screen appears for a second or 2 and disappears. The black screen is the facebook login, but since I’ve already allowed the permission, the black screen disappears immediately. Is this supposed to happen everytime I post a status even after already allowing the permissions? I didn’t change any code for the second tap of the button, should I change this? On succeeding logins, do I still have to use the code below to post even if I already allowed ‘publish_actions’?

facebook.login( fbAppID, listener, {“publish_actions”})

My app does post a status on Facebook. The problem is that even if I’ve coded it to post a status after a successful login, it doesn’t do so after allowing the permissions (1st time login). I’d have to press the button to post the 2nd time so it’ll do so. Is there any special way to include the Play Store app link upon status post on Facebook or I’d just manually include the link so people can be redirected to the Play Store to download my app? Thank you very much.

Your first call should be: 

facebook.login( fbAppID, listener)

Then when you’re ready to post, the first time, then call:

facebook.login( fbAppID, listener, {“publish_actions”})

Set a flag so that you know you’re logged in.  After that you should just need to call facebook.request() to do your activities.

Rob

Is there anyway I’ll know if the user has already allowed the permission to post or not? This should only be used the first time…

facebook.login( fbAppID, listener, {“publish_actions”})

then after the permission is allowed… This is safe to call…

facebook.login( fbAppID, listener)

How do I code it to determine if the permission is already allowed? This way I’ll be able to know which code to execute. Should I call

facebook.login( fbAppID, listener) everytime the app is started? There’s a session token that expires so I think it should be called everytime the app is started or when the user wants to post status on Facebook, right? Thank you Sir.

hello  my app freeze every time i push the button to log in to facebook ive set the correct key hash and everything and even ive set a 1 log in system then a 2 but still it freezes plz help

Hello marc.jd. I’m a newbie too but I’ll gladly help you with everything I can. Are you sure you provided the right key hash? Please show us your code so we can help you debug it. Usually in Corona, when a faulty code is executed, it just does nothing or freezes, at least that’s my experience, in some bigger errors, the app crashes. So you might have coded it wrong. My guess is that you used some variables or functions that have not been initialized yet. It’s almost always the case for me when I do something wrong. I’m a .NET developer and understanding Corona code is not hard but I had to get used to the coding style, Corona reads from top to bottom always.

much appreciated thank u and me too i had to get used to it too and its quite good and here’s my code for the log in and the build setting.

-----build setting-------------

settings =

{

orientation = 

{

default = “landscapeRight”,        supported = 

{

“landscapeRight”,“landscapeLeft”

},

},

android =

{

  googlePlayGamesAppId = “XXXXXXXX”,

usesPermissions =

{

“android.permission.INTERNET”,

},

},

plugins =

{

[“facebook”] =

{

– required

publisherId = “com.coronalabs”,

supportedPlatforms = { iphone = true, [“iphone-sim”] = true },

},

},

  plugins =

{

– key is the name passed to Lua’s ‘require()’

[“CoronaProvider.gameNetwork.google”] =

         {

            --required!

            publisherId = “com.coronalabs”,

            supportedPlatforms = { android = true }

         },

  

 },

}


so the fcb_leaderboard is a scrollview in wich i will add scores of friends after the log in and the facebookconnect integer is to see if user logged in so it will be saved as 1 so each time the app start it will log in to Facebook automatically 

------------code—

local facebook = require( “facebook” )

local json = require( “json” )

local fbCommand = nil

local appId = “XXXXX”

function changemenu3()

    facebookconnect = 1 

   local facebookconnectFilename = “facebookconnect.data”       

  saveValue(facebookconnectFilename, tostring(facebookconnect) )

  connect_fcb.y = 100

  connect_fcb:prepare(“disconnect”)

  connect_fcb:play()

  fcb_leaderboard.x = 132

  cb_leaderboard.y = 270

end

function facebookListener(event)

  print( “Facebook Listener events:” )

            –

        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

                      if(facebookconnect==0)then

                            changemenu3()

                          end

                    end

                    if event.phase == “logout” then

                    end

                    if event.phase == “loginFailed” then

                      function cancelfcbalert(event)

                        if(event.action==“clicked”)then

                          if(event.index == 1)then

                            native.cancelAlert( alert )

                        end

                      end

                      alert = native.showAlert( “”, “Facebook login error. Check connection or try again later.”, { “Done” }, cancelfcbalert )

                    end

                  end

                    – The following displays a Facebook dialog box for posting to your Facebook Wall

                 

        elseif ( “request” == event.type ) then

            local response = event.response

        elseif ( “dialog” == event.type ) then

        end

end

function connect_tofcb(event)

    if(event.phase==“began” and gamestart==false)then

      if(facebookconnect==0)then

        facebook.login( “XXXXXXXXX”, facebookListener , { “public_profile”,“user_friends” }  )

    else

      function areyousurefcb( event )

       if(event.action==“clicked”)then

        if(event.index==1)then

          facebook.logout( )

          facebookconnect = 0

  local facebookconnectFilename = “facebookconnect.data”       

  saveValue(facebookconnectFilename, tostring(facebookconnect) )

  connect_fcb.y = 250

  connect_fcb:prepare(“connect”)

  connect_fcb:play()

  fcb_leaderboard.x = 132

  fcb_leaderboard.y = 2000

        else

          native.cancelAlert( alert )

      end

      end

    end

    alert = native.showAlert( “”, “Are you sure you want to disconnect from this Facebook account?”, { “Yes”, “No” }, areyousurefcb )

end

end

end

connect_fcb:addEventListener(“touch”, connect_tofcb )

and thank you so much

Let’s do this baby steps and one at a time. I suggest you remove codes for now. Start with the very basic. Are you able to sign in to Facebook without accessing the leaderboard? I have removed the extra code I have and here’s my basic Facebook login code, this works because I am using this. If this works for you, then let’s add a little more code again until we get to your goal. I’ve always done this technique which works for me always. This will do a login and post ‘Success’ on your wall.

local facebook = require "facebook" local button1 local function listener( event ) print( "event.name", event.name ) --"fbconnect" print( "event.type:", event.type ) --type is either "session", "request", or "dialog" print( "isError: " .. tostring( event.isError ) ) print( "didComplete: " .. tostring( event.didComplete ) ) if ( "session" == event.type ) then --options are: "login", "loginFailed", "loginCancelled", or "logout" if ( "login" == event.phase ) then local access\_token = event.token facebook.request( "me/feed", "POST", { message="Success" } ) end elseif ( "request" == event.type ) then print("facebook request") if ( not event.isError ) then local response = json.decode( event.response ) --text.text= event.response end elseif ( "dialog" == event.type ) then print( "dialog", event.response ) --text.text= event.response --handle dialog results here end end local function buttonTouch(event) --take in the event and get the associated display object local myButtons = event.target --now find out which button it is local nameString = myButtons.id --use began and ended phases to change text based on touches if event.phase == "began" then --set the label text to the id elseif event.phase == "ended" then if nameString == "button0" then elseif nameString == "button1" then facebook.login( fbAppID, listener, {"publish\_actions"}) end end return true end button1 = widget.newButton{     id = "button1",     label = "Login to Facebook",     emboss = false,     shape = "roundedRect",     left = 1000,     top = ((display.contentHeight/10)\*6)-20,     width = controlwidth,     height = controlheight,     cornerRadius = 2,     fillColor = { default={ 1, 0.2, 0.5, 0.7 }, over={ 1, 0.2, 0.5, 1 } },     strokeWidth = 4,     onEvent = buttonTouch }

no still it also freeze and then crash because its not responding i should also mention that with my codes it works one time then after it freezes after a couple of hour and maybe a day it work again if u want i could send u the apk to see it for yourself if u want?

Sure thing, just send me the whole folder of your app, I’ll compile it myself if you don’t mind. I’ll check everything and report back to you. Remove any secret data you have before sending it to me. I’m not going to use any of your secret data but I want to play safe because I don’t want to get in any trouble. My email is chris_techno25@yahoomail.com. I have problems with Facebook myself but not like your problem, I’m done with your trouble. In fact, it’s the only thing left to do on my app. I finished my app in a week and I’ve got so much delay because of Facebook, all the trouble just to get the app to post a score. By the way, what phone do you have? I have a Nexus 5.

i have a galaxy note 3 and my app is already on play store but the next update include a global and a friends leaderboard i managed the global leaderboard the only thing delaying me is the friends one here’s a link to the one on play store https://play.google.com/store/apps/details?id=com.live.Mjay.games.FellinHell_android  

and am gonna give u the link of the one am working on by drop box not email

https://www.dropbox.com/s/52kq72vvq7d73fm/FellinHell.apk?dl=0

i think corona has some bugs in facebook cause my login sometime works sometimes no…and what do u mean by posting a score like in a status? maybe i could help?

Well I can post status on Facebook fine, my problem is that the first time I login, it doesn’t do so even if it’s coded to do so. My app can post the status the second time I tap the post button. It’s weird because it should post the first time. And one more thing that annoys me, everytime I tap the post button, a black screen appears and disappears, this is the facebook login. This is after the 1st login. Should this happen all the time? And how do I post a word as status that when clicked, links to the android play store? Thank you very much. I will test your code and report back to you. Although it might not be now because it’s already midnight here. But I will post back any result I get I promise. By the way, there’s no way I can debug your code if you only send me an apk file. Please do send me your code, just remove any of your secret game code and leave the facebook stuff. Make sure it works already, so I don’t have to check everything if something does not run.

yeh ill give u the code as soon as i can(am not home right now) but tell me did it work can u log in?

and did u request permission to “publish_actions” when logging  in after u have to request to post like

local postMsg = {    
        message = “your message”,
        source = {
baseDir=system.ResourceDirectory,
filename=“imge source”,
type=""},
            }
        
facebook.request( “me/feed”, “POST”, postMsg )

Hi Marc, actually no, when I login it doesn’t freeze, but it does nothing at all. I’m not sure what the problem of your app is, but we can solve that by reviewing your code, I’ll wait for it. Yeah, I did that code you sent me but still the same. By the way, how did you get your app to be approved by Facebook. I submitted my app for approval twice and Facebook still disapproved it because I lack information. I submitted screenshots and step by step instruction. Facebook sent me these things that I should have…The screenshot is in Dropbox. Facebook says I need to have Facebook Login. Do you have to have a separate LOGIN button and posting? Thank you very much. I’m not really having that much trouble with the Facebook Corona codes, my app just doesn’t get approved, but almost everything works fine, just that annoying black screen, but it still does the thing. Yeahp I did request ‘publish_actions’ and it works, the problem is that the first time I login it doesn’t do so, only the 2nd time. This can be solved by adding some additional codes but I was just wondering why this happens so even if I coded it to post even for the 1st time the user logs in. I’ll investigate on this further.

https://www.dropbox.com/s/jdy635cqwsh9rdv/Untitled.jpg?dl=0

You should do two separate logins.  The first one asks for basic permissions.  Then when they press the button, it would login a second time asking for the extended permissions.  This is a Facebook requirement.  You can look at the Corona Labs Facebook sample code in SampleCode/Networking/Facebook/.

Then beyond that you should look at your device’s console log and look for any warning or errors.  If you need help with that, see:

http://coronalabs.com/blog/2013/07/09/tutorial-basic-debugging/

Make sure to run the “adb logcat” command with no other parameters that would filter just Corona messages.  You need to see the Facebook messages as well.

Rob

Hello Sir. Thank you for your reply. The first time I log in with this code…

facebook.login( fbAppID, listener, {“publish_actions”})

It gets me to a 2 process login like you said, the 1st one for basic permissions and the 2nd one for ‘publish_actions’. On succeeding button taps after the 1st one, a black screen appears for a second or 2 and disappears. The black screen is the facebook login, but since I’ve already allowed the permission, the black screen disappears immediately. Is this supposed to happen everytime I post a status even after already allowing the permissions? I didn’t change any code for the second tap of the button, should I change this? On succeeding logins, do I still have to use the code below to post even if I already allowed ‘publish_actions’?

facebook.login( fbAppID, listener, {“publish_actions”})

My app does post a status on Facebook. The problem is that even if I’ve coded it to post a status after a successful login, it doesn’t do so after allowing the permissions (1st time login). I’d have to press the button to post the 2nd time so it’ll do so. Is there any special way to include the Play Store app link upon status post on Facebook or I’d just manually include the link so people can be redirected to the Play Store to download my app? Thank you very much.

Your first call should be: 

facebook.login( fbAppID, listener)

Then when you’re ready to post, the first time, then call:

facebook.login( fbAppID, listener, {“publish_actions”})

Set a flag so that you know you’re logged in.  After that you should just need to call facebook.request() to do your activities.

Rob

Is there anyway I’ll know if the user has already allowed the permission to post or not? This should only be used the first time…

facebook.login( fbAppID, listener, {“publish_actions”})

then after the permission is allowed… This is safe to call…

facebook.login( fbAppID, listener)

How do I code it to determine if the permission is already allowed? This way I’ll be able to know which code to execute. Should I call

facebook.login( fbAppID, listener) everytime the app is started? There’s a session token that expires so I think it should be called everytime the app is started or when the user wants to post status on Facebook, right? Thank you Sir.

hello  my app freeze every time i push the button to log in to facebook ive set the correct key hash and everything and even ive set a 1 log in system then a 2 but still it freezes plz help