Facebook posting on Android

With your suggestion, moving the request to the listener, still not working.  I get the same android screen that says Facebook: my game would like to access your public profile and friends list, hit ok, then nothing happens.

logcat shows:

I/Corona  ( 1580): event.name   fbconnect

I/Corona  ( 1580): event.type:  session

I/Corona  ( 1580): isError: false

I/Corona  ( 1580): didComplete: nil

any other thoughts?

Interestingly there are like three threads reporting a similar issue, which would lead me to suspect there is a problem  I’ve spent my time today constructing a very basic Facebook app.  I’ve verified that it runs correctly on both iOS 7 (iPhone 5) and on Android 4.4 (Google Nexus 7).

build.settings

settings = {     orientation =     {         default = "portrait",         supported = { "portrait" }     },     android =     {         usesPermissions =         {             "android.permission.INTERNET",         },     },     plugins =     {         -- key is the name passed to Lua's 'require()' This section is only required for builds \> 2014.2165         ["facebook"] =         {             -- required             publisherId = "com.coronalabs",             supportedPlatforms = { iphone = true, ["iphone-sim"] = true },         },     },     iphone =     {         plist =         {             CFBundleIconFile = "Icon.png",             CFBundleIconFiles =             {                 "Icon.png",                 "Icon@2x.png",                 "Icon-60.png",                 "Icon-60@2x.png",                 "Icon-72.png",                 "Icon-72@2x.png",                 "Icon-76.png",                 "Icon-76@2x.png",                 "Icon-Small.png",                 "Icon-Small@2x.png",                 "Icon-Small-40.png",                 "Icon-Small-40@2x.png",                 "Icon-Small-50.png",                 "Icon-Small-50@2x.png",             },             UIApplicationExitsOnSuspend = false,    -- must be false for single sign-on to work             FacebookAppID = "XXXXXXXXXXXXXX",  -- replace XXXXXXXXX with your facebook appId             CFBundleURLTypes =             {                 {                     CFBundleURLSchemes =                     {                         "fbXXXXXXXXXXXXXX",    -- replace XXXXXXXXX with your facebook appId                                              }                 }             }         }     } }

main.lua

io.output():setvbuf('no') local widget = require( "widget" ) local json = require( "json" ) local facebook = require( "facebook" ) local facebookAppID = "XXXXXXXXXXXXXXX" local FB\_Command = nil function print\_r ( t )     local print\_r\_cache={}     local function sub\_print\_r(t,indent)         if (print\_r\_cache[tostring(t)]) then             print(indent.."\*"..tostring(t))         else             print\_r\_cache[tostring(t)]=true             if (type(t)=="table") then                 for pos,val in pairs(t) do                     if (type(val)=="table") then                         print(indent.."["..pos.."] =\> "..tostring(t).." {")                         sub\_print\_r(val,indent..string.rep(" ",string.len(pos)+8))                         print(indent..string.rep(" ",string.len(pos)+6).."}")                     elseif (type(val)=="string") then                         print(indent.."["..pos..'] =\> "'..val..'"')                     else                         print(indent.."["..pos.."] =\> "..tostring(val))                     end                 end             else                 print(indent..tostring(t))             end         end     end     if (type(t)=="table") then         print(tostring(t).." {")         sub\_print\_r(t,"  ")         print("}")     else         sub\_print\_r(t,"  ")     end     print() end local function facebookListener( event )     print\_r( 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             --code for tasks following a successful login             print( 'Token: ' .. access\_token )             if FB\_Command then                 facebook.request( FB\_Command )             end         end     elseif ( "request" == event.type ) then         print("facebook request")         if ( not event.isError ) then             local data = json.decode( event.response )             print( "data: ", data )             print( "event.response: ", event.response )         else             print( "event.isError: " .. event.isError )         end     end end local function doFacebook( event )     if event.phase == "ended" then         FB\_Command = "me"         facebook.login( facebookAppID, facebookListener, {"publish\_actions", "email" })     end end local fbButton = widget.newButton({     label = "Facebook",     onEvent = doFacebook     }) fbButton.x = display.contentCenterX fbButton.y = display.contentCenterY facebook.login( facebookAppID, facebookListener )

This should be minimalist Facebook app.  It does a 2 pass login, the first time requesting basic permissions, then when you hit the button, it asks for additional permissions.  Once your app runs and these have been granted you won’t be prompted again.   The things printed to the console log are the things I would expect to see.

You can use this or use the Sample app in the SampleCode/Network/Facebook folder to verify your settings.

I will be up front and was very frustrated because it just wasn’t working for me.  Then I used the build.settings from the sample app replacing my build.settings.  It suddenly started working.  This is another thing you can try.  Replace your build.settings with the one from the sample app.

Rob

I will give this a try, my test android device is a galaxy s2 version 4.1.2

Any chance the simple “social” plug-in to post to Facebook will work on Android? I appreciate the code above and would probably implement it, but for me, it was dead easy to implement my ‘share to email/text/Twitter/Facebook feature’ in my game using the native.showPopup method without having to even set up an app ID on Facebook.

There is an outstanding bug with Android and Facebook that prevents Android Sharing Intent’s from posting to Facebook.  So the only way to get Android apps to post to Facebook is to use the Facebook SDK.  Since this Intent doesn’t work, we filter it out.

Used your code Rob, and the build.settings from the scrumptious app, now I am getting 

V/Corona (11269): \> Class.forName: CoronaProvider.ads.admob.LuaLoader V/Corona (11269): \< Class.forName: CoronaProvider.ads.admob.LuaLoader V/Corona (11269): Loading via reflection: CoronaProvider.ads.admob.LuaLoader I/Corona (11269): BUILD NUMBER IS: 2189 I/Corona (11269): USING GRAPHICS OUTLINES: true I/Corona (11269): ADMOB AD - Event: The ad loaded successfully I/Corona (11269): table: 0x5f5830a8 { I/Corona (11269): [type] =\> "session" I/Corona (11269): [name] =\> "fbconnect" I/Corona (11269): [phase] =\> "loginCancelled" I/Corona (11269): [isError] =\> false I/Corona (11269): [response] =\> "" I/Corona (11269): }

In one of the other threads they found that not having the facebook native app but having the facebook messanger app caused this.  Take the Corona filter off of the adb logcat (i.e. run adb logcat with no parameters) and see if you’re getting any errors from other intents.

Rob

Hi everyone, seem i got this issue happen while try to login with facebook via android device, it seem login function did not work,

but all my facebook App configure seem got everything in android platform, package name, class, and key hash,

but i cant event login with that, or do i miss anything with Facebook App?

it always return below value

03-18 16:57:03.897: I/Corona(24463):   type    session

03-18 16:57:03.897: I/Corona(24463):   name    fbconnect

03-18 16:57:03.897: I/Corona(24463):   phase    loginCancelled

03-18 16:57:03.897: I/Corona(24463):   isError    false

03-18 16:57:03.897: I/Corona(24463):   response    

Run your adb logcat without any parameters.  The way most people learn to run adb logcat only shows messages from the Corona app, not other intents like Facebook and look for other errors. 

Another poster found the Facebook Messenger app was at fault.  Another found that his hash key really wasn’t right afterall.   All of these were found by running adb logcat and looking for non-Corona messages.

Rob

@Rob does it require our App on Android Store or not to make this work?

You will want it in Google Play so Facebook can link to it to let people download it.  But its not required for testing.

Rob

Thank you Rob, one more thing that i am not clear with Facebook api as when I post a link to Facebook I need the link could be open an App if App installed and go to download if not. How can we do that?

Sorry Rob, I have had to be offline a few days. I dont have a facebook messenger app installed. I was still getting api errors when I would output my logcat to a text file.(I mean thats what the file was reporting). So I read in another forum where the problem was with facebook applications and to delete the app and make a new one.  So this is where it gets interesting, I did that on Monday, and for the past 2 days after that I am now getting Hash key errors, but have the same hash key o_0. Im almost at my wits end here.

cap2.jpg

cap3.jpg

Facebook knows if your app is installed or not.  I would presume they manage to try and load your app for you if you tap on it from the Facebook mobile app.  This is one of the uses for providing the URL scheme on iOS.  On Android, it uses the App’s reverse domain name.  If the person is on the browser, then it will use information you put in your portal to direct you to the right app store.

However any links you provide will likely only be links to web based resources.

@david.newman, that message is quite clear.  Your hash key isn’t right.  Some people are able to take the string from the console log and just use it.  Facebook is telling you what they want.  Have you tried that?

yeah Rob, hence the screenshots, they are the same

= \

All of my hashkey’s end in an = sign.  Try adding it with the = on the end.  I don’t think Facebook prints it with the = sign.

o.0 you’re right, what the eff…

seriously I apologize for that last post…its been a long week.  Let me find my device and give a try.

ok, so unless I need to wait another 24 hours after adding = to that hash key, getting the same error.