loginWithFacebook not working

Hi,

trying to login user with facebook in my MainActivity, and Android Studio won’t understand the command.

in the line “Backendless.UserService.loginWithFacebook(MainActivity.this, facebookFieldMappings, permissions, new AsyncCallback<BackendlessUser>()” and so on, it show the error red underline, as you can see in the screenshot.
what did I do wrong ?
thanks, Gil.

Hi, Gil.
As you can see in loginWithFacebook method signature (left click on method name with Ctrl pressed):
public void loginWithFacebook(Activity context, WebView webView, Map<String, String> facebookFieldsMappings, List<String> permissions, AsyncCallback<BackendlessUser> responder)
it takes WebView as a second parameter. In your case, you can use “null” instead of webView.

Dear Anatolii,

Thanks, I can compile now, but then I try to get the user’s mail and first_name from facebook, nothing happens.
This is my code, to my understanding it should work… :

The problem is with the connection to facebook.
even though I inserted the facebook app ID and facebook app secret, when I run my app it says =
“Given URL is not permitter by the Application configuretion: One or more of the gibel URLs is not permitted by the App’s settings. It must match the Website URL or Canvas URL, or the domain must be a subdomain of one of the Apps domains”

what did I do wrong ?
thanks!

If you do not set Facebook App ID and App Secret then these credentials will be used from our default app on Facebook. You can try it out (without Facebook credentials), just to see how it should be working.
If you provide credentials of your own Facebook application, than you are responsible for its correct settings. You can contact Facebook documentation https://developers.facebook.com/docs/android/getting-started on that.
As it looks to me, you should provide correct URL settings in your Facebook app - navigate your Facebook app settings and check “App Domains”, “Site URL” (you’ll have to choose “Add platform | Web Site” first) and “Valid OAuth redirect URIs” (on “Advanced” tab), they should say “https://api.backendless.com/

Dear Anatolii,

I’ve removed m own app ID and secret (left it blank), and inserted the https://api.backendless.com/ as a website and in the Valid OAuth redirect URIs in the advanced tab.

now my app says “could not approve social account.” any ideas ?

oh wait now that I removed the ID and secret of the facebook app, it’s not connected to the app :slight_smile:

but it should work with your default app on facebook, no ?

Gil, you can use the CodeGeneration sample for Facebook login. In Backendless Console navigate to tab “Codegeneration” / “Android”. In IDE dropdown you can choose “Intellij Idea”, check Facebook Login and press Download Project button below. You can use these java classes a Facebook Login sample.

Thanks Anatolii but I can’t seem to open it right with Android Studio. it only shows the java classes and not the layouts, res etc.

anyways it’s similar to my own code. which seems to work fine except for the fact the it the Backendless website I see a long number where the user’s mail should be, and I get no detail about the user from facebook : first_name, etc.

CodeGeneration feature for Android Studio will be released shortly.
Right now you can you the project I’ve attached (be sure to replace APPLICATION_ID and SECRET_KEY in file Defaults.java)

backendless-codegen.zip (0.9MB)

Thanks dear Anatolii, you’re the best.

it works like my code - it loggs into facebook but doesn’t return any facebook user details.

Did you try to put breakpoint into hadleResponse method of the callback you pass to loginWithFacebook method?

Like this:

public void onLoginWithFacebookButtonClicked()
{
Backendless.UserService.loginWithFacebook( LoginActivity.this, new SocialCallback<BackendlessUser>( LoginActivity.this )
{
@Override
public void handleResponse( BackendlessUser backendlessUser )
{
// PUT BREAKPOINT HERE and check the backendlessUser entity you receive
startActivity( new Intent( getBaseContext(), LoginSuccessActivity.class ) );
finish();
}
} );
}

Dear Anatolii, I’m not sure what you mean by breakpoint.

I use this code, which to my best knowledge suppose to get the facebook users’ email and first name:

Map<String, String> facebookFieldMappings = new HashMap<String, String>();
facebookFieldMappings.put( “email”, “email” );
facebookFieldMappings.put( “first_name”, “first_name” );

List<String> permissions = new ArrayList<String>();
permissions.add( “email” );
permissions.add( “first_name” );

Backendless.UserService.loginWithFacebook(MainActivity.this, null, facebookFieldMappings, permissions, new AsyncCallback<BackendlessUser>()
{
@Override
public void handleResponse( BackendlessUser backendlessUser )
{
// user logged in successfully
Intent intent = new Intent( MainActivity.this, FeedActivity.class );
startActivity( intent ); // go to the user feed activity in my app
}

@Override
public void handleFault( BackendlessFault backendlessFault )
{
    // failed to log in
}

} );

It logs in succesfully (I see the user the Backendless webpage) but I only get a long number as an email and that’s it.

Gil, you use permission “first_name” which not valid. Here’s the list of valid Facebook permissions: https://developers.facebook.com/docs/facebook-login/permissions
The code you provided should result in error "Invalid scope “first_name”.

The case that you descibed (getting id number in email field) is valid if you do not add “email” permission.
Adding these two factors it looks to me that the code you provided is not the code that you actually run(looks like you run the loginWithFacebook(Activity context, AsyncCallback<BackendlessUser> responder) method, without permissions and fieldsMapping).
Please check that again

Dear Anatolii,

it took me 1 minute to fix the code, and 59 minutes to understand that I need to delete the “numbered” user and register again.
works perfectly. I thank you!

Dear Anatolii,

Is it possible to for a user to search other users for an attribute, like hometown?

And is it possible to create a push notification when the user changes an attribute like “current city” to all his followers?

I’m asking because I didn’t find it in the Backendless Android PDF.

Thanks! Gil.

Hi Gil!
Here you can find useful information about search implementation: https://backendless.com/documentation/users/rest/users_get_user_properties.htm

https://backendless.com/documentation/data/rest/data_search_and_query.htm

And is it possible to create a push notification when the user changes an attribute like “current city” to all his followers?
For this case you can use business logic. More information you can get from this documentation:
https://backendless.com/documentation/business-logic/java/bl_overview.htm
Regards,
Kate.

Dear Kate, thanks, I will read the links :slight_smile:

Hi,

Done with login with facebook but how can i get data from handleResponse success?

Thanks