One the user has authorized your app for Facebook, you can do facebook.request(“me/”, “GET”). This will return, among other things, the user’s facebook ID, their name, and some other public details of their profile.
- Andrew
One the user has authorized your app for Facebook, you can do facebook.request(“me/”, “GET”). This will return, among other things, the user’s facebook ID, their name, and some other public details of their profile.
when i start my app it asks me that the app would like to access my profile and friend lists and when i allow i don’t get anything.
this is my code, could you please see what is wrong with it???
It is supposed to display Facebook id and name…
-- ============================================================= -- Copyright Roaming Gamer, LLC. 2009-2013 -- ============================================================= -- ============================================================= -- -- ============================================================= local storyboard = require( "storyboard" ) local scene = storyboard.newScene() local facebook = require( "facebook" ) local json = require "json" --local debugLevel = 1 -- Comment out to get global debugLevel from main.cs local dp = ssk.debugPrinter.newPrinter( debugLevel ) local dprint = dp.print ---------------------------------------------------------------------- -- LOCALS -- ---------------------------------------------------------------------- -- Variables local screenGroup local layers -- Callbacks/Functions local onBack local function listener( event ) if ( "session" == event.type ) then -- upon successful login, request list of friends of the signed in user if ( "login" == event.phase ) then facebook.request( "me/" ) -- Fetch access token for use in Facebook's API local access\_token = event.token print( access\_token ) end elseif ( "request" == event.type ) then -- event.response is a JSON object from the FB server local response = event.response -- if request succeeds, create a scrolling list of friend names if ( not event.isError ) then response = json.decode( event.response ) local data = response.data if(data[i].id) then local fId = data[i].id ssk.labels:quickLabel( layers.buttons, fId, 250, 100, nil, 30 ) print( name ) else ssk.labels:quickLabel( layers.buttons, "No id", 250, 100, nil, 30 ) end if(data[i].name) then local name = data[i].name ssk.labels:quickLabel( layers.buttons, name, 250, 300, nil, 30 ) print( name ) else ssk.labels:quickLabel( layers.buttons, "No name", 250, 100, nil, 30 ) end end elseif ( "dialog" == event.type ) then print( "dialog", event.response ) end end ---------------------------------------------------------------------- -- Scene Methods: -- scene:createScene( event ) - Called when the scene's view does not exist -- scene:willEnterScene( event ) -- Called BEFORE scene has moved onscreen -- scene:enterScene( event ) - Called immediately after scene has moved onscreen -- scene:exitScene( event ) - Called when scene is about to move offscreen -- scene:didExitScene( event ) - Called AFTER scene has finished moving offscreen -- scene:destroyScene( event ) - Called prior to the removal of scene's "view" (display group) -- scene:overlayBegan( event ) - Called if/when overlay scene is displayed via storyboard.showOverlay() -- scene:overlayEnded( event ) - Called if/when overlay scene is hidden/removed via storyboard.hideOverlay() ---------------------------------------------------------------------- function scene:createScene( event ) screenGroup = self.view -- Create some rendering layers layers = ssk.display.quickLayers( screenGroup, "background", "buttons", "overlay" ) -- AWESOME CONTENT HERE local appId = myAppId if ( appId ) then facebook.login( appId, listener ) end ssk.buttons:presetPush( layers.buttons, "default", 55, h-25, 100, 40, "Back", onBack ) if(build\_settings.orientation.default == "landscapeRight") then ssk.labels:quickLabel( layers.buttons, "Login", 50, 100, nil, 30 ) else ssk.labels:quickLabel( layers.buttons, "Login", 50, 100, nil, 22 ) end end ---------------------------------------------------------------------- ---------------------------------------------------------------------- function scene:willEnterScene( event ) screenGroup = self.view end ---------------------------------------------------------------------- ---------------------------------------------------------------------- function scene:enterScene( event ) screenGroup = self.view end ---------------------------------------------------------------------- ---------------------------------------------------------------------- function scene:exitScene( event ) screenGroup = self.view end ---------------------------------------------------------------------- ---------------------------------------------------------------------- function scene:didExitScene( event ) screenGroup = self.view end ---------------------------------------------------------------------- ---------------------------------------------------------------------- function scene:destroyScene( event ) screenGroup = self.view layers:removeSelf() layers = nil screenGroup = nil end ---------------------------------------------------------------------- ---------------------------------------------------------------------- function scene:overlayBegan( event ) screenGroup = self.view local overlay\_name = event.sceneName -- name of the overlay scene end ---------------------------------------------------------------------- ---------------------------------------------------------------------- function scene:overlayEnded( event ) screenGroup = self.view local overlay\_name = event.sceneName -- name of the overlay scene end ---------------------------------------------------------------------- -- FUNCTION/CALLBACK DEFINITIONS -- ---------------------------------------------------------------------- onBack = function ( event ) ssk.debug.monitorMem() local options = { effect = "fade", time = 200, params = { logicSource = nil } } storyboard.gotoScene( "s\_MainMenu", options ) return true end --------------------------------------------------------------------------------- -- Scene Dispatch Events, Etc. - Generally Do Not Touch Below This Line --------------------------------------------------------------------------------- scene:addEventListener( "createScene", scene ) scene:addEventListener( "willEnterScene", scene ) scene:addEventListener( "enterScene", scene ) scene:addEventListener( "exitScene", scene ) scene:addEventListener( "didExitScene", scene ) scene:addEventListener( "destroyScene", scene ) scene:addEventListener( "overlayBegan", scene ) scene:addEventListener( "overlayEnded", scene ) --------------------------------------------------------------------------------- return scene
It looks like the portion of your Facebook listener that deals with requests (the part that starts with the line “elseif ( “request” == event.type ) then”) is wrong. You decode the response and then set data = response.data, which is fine. But then you attempt to access data as if it were an array, using an index, i, that you haven’t defined. (Might you have copied that code from somewhere else?)
Instead, try simply printing event.response, so you can see what the JSON structure looks like before accessing it.
you are right @aukStudio, I have copy pasted the code from the corona online docs and modified for testing. Yap i forgot to remove that array stuff but still it should have worked because in each if i added an else to tell me that the statements failed.
Anyway I removed those array stuffs and still no response…
NOTE: ssk.labels:quickLabel displays stuff on device’s screen.
local function listener( event ) if ( "session" == event.type ) then -- upon successful login, request list of friends of the signed in user if ( "login" == event.phase ) then facebook.request( "me/" ) -- Fetch access token for use in Facebook's API local access\_token = event.token print( access\_token ) end elseif ( "request" == event.type ) then -- event.response is a JSON object from the FB server --local response = event.response response = json.decode( event.response ) if ( not event.isError ) then response = json.decode( event.response ) local data = response.data ssk.labels:quickLabel( layers.buttons, data, 250, 500, nil, 30 ) if(data.id) then local fId = data.id ssk.labels:quickLabel( layers.buttons, fId, 250, 100, nil, 30 ) print( fId) else ssk.labels:quickLabel( layers.buttons, "No id", 250, 100, nil, 30 ) print("no fId") end if(data.name) then local name = data.name ssk.labels:quickLabel( layers.buttons, name, 250, 300, nil, 30 ) print( name ) else ssk.labels:quickLabel( layers.buttons, "No name", 250, 100, nil, 30 ) print("no name") end else ssk.labels:quickLabel( layers.buttons, "event.error", 250, 500, nil, 30 ) print("error is true") end elseif ( "dialog" == event.type ) then print( "dialog", event.response ) end end
Are you sure the login itself is succeeding? Do you get the access token? (I see that you are printing it – do you see that print of the access token in the console)?
Are you testing on an iOS device or an Android device?
I don’t know how to see the prints in devices but i tried to display the access token using the newText but strangely nothing appears in the screen after facebook login as if the listener is never called?
It is several days now i can’t fucking login to the facebook, it is getting very annoying 
UPDATE:
I added these 3 lines in the beginning of the listener before any if checks, and i got some responses…
local myText = display.newText("listener", 250, 50, native.systemFont, 30) local myText2 = display.newText(event.type, 250, 150, native.systemFont, 30) local myText3 = display.newText(event.phase, 250, 250, native.systemFont, 30)
Response:
listener session loginCancelled
Now what is “loginCancelled” and why it happent?
PS: I just created the app using my regular Facebook account and coppied the appID, I donno if there is any extra setting i should setup in my Facebook app?
Hi there,
First of all, you should definitely make sure you know how to view print statements when on the device. It’s very easy, just read this blog post: http://www.coronalabs.com/blog/2013/07/09/tutorial-basic-debugging/. It’s much better than using display.newText().
As for the Facebook setup, have you followed all of the steps in these guides: http://docs.coronalabs.com/guide/social/setupFacebook/index.html and http://docs.coronalabs.com/guide/social/implementFacebook/index.html#facebook?
Yeah, i have followed all those fucking steps and still i receive loginCancelled…It is getting really annoying.
Someone help please.
Are you testing on an iOS or Android device? What version of iOS or Android is the device running? Is the Facebook app installed?
It would also help if you copy/pasted your build.settings into this thread, and a screenshot of your app’s setup in the Facebook developer portal, so that we can make sure you set it up correctly.
I am running on a android Samsung Galaxy S2 device.
build.settings
-- For more details on this file and what you can do with it, look here: -- https://docs.coronalabs.com/guide/distribution/buildSettings/index.html -- settings = { orientation = { default = "landscapeRight", --content = "landscapeRight", -- Un-comment to lock to this orientation on iOS ONLY supported = { "landscapeLeft", "landscapeRight", }, }, android = { -- The version code must be an integer — it cannot contain any decimal points. versionCode = "11", -- Increase heap size from 32MB to device max (this setting is for Android 3.0 and higher. -- Android 2.x devices will still run the app but they'll ignore this setting.) largeHeap = true, usesPermissions = { "android.permission.INTERNET", "android.permission.WRITE\_EXTERNAL\_STORAGE", --"android.permission.ACCESS\_FINE\_LOCATION", --"android.permission.ACCESS\_COURSE\_LOCATION", -- Find more permissions here: http://developer.android.com/reference/android/Manifest.permission.html }, usesFeatures = { --{ name = "android.hardware.camera", required = true }, --{ name = "android.hardware.location", required = false }, --{ name = "android.hardware.location.gps", required = false }, -- Find more features here: http://developer.android.com/guide/topics/manifest/uses-feature-element.html#features-reference } }, iphone = { plist = { UIAppFonts = { "Harrowprint.ttf", }, UIStatusBarHidden = true, UIPrerenderedIcon = false, --CFBundleIconFiles = { }, --MinimumOSVersion = "5.1.0", }, }, }
Facebook app setup:

Android Build:

OK, that all looks fine.
Do you have the Facebook app installed on your device? When your app attempts to login to Facebook, what happens? Does it switch over to the Facebook app or show a popup asking for permission? Also, in your code, where are you setting myAppId? Is it a global defined in another module? Make sure you define it as a string, not as an integer.
I have facebook app installed but i get a popup message asking for permission, i tried both my developer fb account and another account.
myAppId is a string locally defined before calling the fb, u can see the code above.
In the code you posted, you have a line local appId = myAppId. However, myAppId is not defined in the code you posted. That’s why I assumed it’s a global defined in another file. Is it? Or is myAppId not a variable but just a placeholder that you wrote in place of “2313xxxxxx69”?
yeah Andrew, it is just a placeholder for “xxxxxxxxxxx”
PS: as u may notice in the android build screenshot, the app name in the build and in the fb setup page is not the same is it ok?
It’s fine if the app name’s are different. All that matters is that the package name is correct.
Can you post a screenshot of the popup that displays asking for permission? Also, you may want to deauthorize the app first, so that you can re-experience the new user permission flow. You can do that by just opening Facebook in a browser as usual, going to Account Settings, then Apps, and then removing the app from the list.