Skip to content

Social and OAuth2 Logins

Backendless provides integration with multiple login providers, including social networks and various cloud/enterprise systems. You can see available login providers in the Backendless Console by clicking the Users icon and then the Login Providers section:

oauth-providers.zoom80

For most providers the configuration screen consists of the following elements:

  • Reference in code - this is a value that should be used in the Backendless API call(s) documented below. It uniquely identifies the provider in the Backendless system.
  • Callback URL - This is a URL that the login provider will use to pass the authentication flow from its system to Backendless. The URL should be copied/pasted into the login provider configuration.
  • Client ID (or Web Client ID or App Key) -  a unique value that should be obtained from the login provider side.
  • Client Secret (or Web Client Secret or App Secret) - a unique value that should be obtained from the logic provider side.

In order to use a login provider, it must be enabled first. To enable a login provider, select it from the list, fill out the form with the required values and click the toggle that says "This provider is OFF". Make sure to click the SAVE button to save the configuration parameters and the ON/OFF state of the provider.

Once a login provider is enabled, your application can authenticate the users through the provider. Backendless supports two different authentication flows:

Login With Provider's SDK

This approach relies on the login provider's implementation to handle the step of displaying the login screen and collecting the user's credentials. The provider SDK hands of the authentication process by delivering an access token that should be sent to Backendless to exchange it for a Backendless User object.

See the illustrations below demonstrating the flow:

Step 1. Your app initiates the login process using provider's SDK. The result of this step is the login user interface:

oauth-sdk-step1

Step 2. User enters their credentials and submits the login form. The result of this step is the authentication access token.

oauth-sdk-step2

Step 3. Your application uses Backendless API to login the user to Backendless using access token. This step results in a user record being created in the Backendless system (if this is a new user). The access token is "exchanged" for Backendless user session. To implement this step use the OAuth2 Login API:

oauth-sdk-step3

Login Without Provider's SDK

With this approach the provider's SDK is not required. The login form is rendered using HTML/JS. If used in a native mobile environment, it will be necessary to use mobile browser component to render the authentication screen.

Step 1. Your application uses Backendless API to receive a URL for the provider's authentication form. To implement this step use the Get Authorization URL API:
oauth-nosdk-step1

Step 2. Your application opens the login form using the URL from step 1. There is no API for this step, your application needs to use the URL to open it in a web view/browser:

oauth-nosdk-step2

Step 3. User logs in with their credentials and submits the form. If the authentication is successful, the login provider responds with a redirect to the Backendless callback URL (this is the URL you copied/pasted from Backendless Console into provider's configuration):

oauth-nosdk-step3

Step 4. The redirect to the Backendless callback URL in the previous step results in the Backendless User object and Backendless user token sent to the application. A Backendless user record is created in the Users table (if this is a new user). There is no API for this step, your application needs to process the result of the form submission and response from the redirect.

oauth-nosdk-step4

Login Provider Configuration

Configuring a login provider will vary as all providers have their own way for obtaining configuration properties. Below you will find instructions for most commonly used social login providers:

Configuring "Login with Facebook"
  1. Navigate to https://developers.facebook.com/
  2. Select an application or create one using the My Apps dropdown in the upper right corner.If you are creating a new app, you will see a popup asking "how are you using your app?". Select the For Everything Else answer.
  3. Add Facebook Login to your app
    add-facebooklogin.zoom50
  4. Locate the App ID and App Secret values in the Settings > Basic section and copy the values into the corresponding fields in the Backendless Console (Users > Login Providers > Facebook).
  5. Click Add platform and select Android.
  6. Enter your Class Name, Package Name and Key Hashes.
    android-facebook-setup.zoom50
  7. Click Save Changes.
  8. Click the Settings tab on the Facebook Product section.
  9. Scroll down to the Client OAuth Settings block and enter https://api.backendless.com into the Valid OAuth redirect URIs field.
  10. In a separate browser window login to Backendless console and navigate to Users > Login Providers > Facebook screen. You will see the Callback URL field. Copy the URL from that field into Valid OAuth redirect URIs (by the time you are done with this step, that field will have two URIs as shown in the screenshot below):
    callback-url-setup-facebook.zoom50
  11. Scroll down to the bottom of the page and click Save Changes.
Configuring "Login with Google"
  1. Navigate to https://console.developers.google.com
  2. Choose an existing project or create a new one.
    new-google-project
  3. Click the Library menu item and search/select Google+ API:
    add-google-plus-oauth
  4. Click Enable to enable the API:
    enable-google-plus-oauth
  5. Once the API is enabled, click the Credentials menu item on the left and then click the CONFIGURE CONSENT SCREEN button:
    configure-consent-screen-oauth.zoom80
  6. Complete all the steps in the OAuth consent screen forms.
  7. Return to the APIs  & Services screen for your project and select the Credentials menu on the left and click +Create Credentials > Create OAuth client ID.
    create-oauth-client-google.zoom80
  8. Select Android.
  9. Enter the application's package name.
  10. Copy the command starting with keytool...using the copy icon:
    keytool-command-google
  11. Open a command prompt/terminal window. Run the keytool command, but make sure to set the value for the -keystore argument. The value must be the proper path to the debug or the production keystore. For example, for the Android apps, the default location of the keystore is ~/.android/debug.keystore, the default password is android.
  12. The output of the keytool command includes the SHA-1 certificate fingerprint. Copy its value and paste into the field on the Add credentials to your project screen located right below the keytool command box.
  13. Enter the package name which you will use in your application and click Create client ID.
  14. Navigate to https://developers.google.com/mobile/add
  15. Click the Add Project button
  16. Select the application you created from the drop-down list.
  17. Click Continue > Continue > Add Firebase.
  18. Select a platform (Android) for the application.
  19. Add configs and click Create application.
  20. Click Download google-services.json. and add it to your Android Stuido project.
  21. Return to https://console.developers.google.com
  22. Select the project and click Credentials.
  23. The credentials should now include a "Web client" which is automatically generated by Google.
  24. Click "Web client" to edit it.
  25. Click the + ADD URI button in the Authorized redirect URIs section and enter the Callback URL value from Backendless Console:
    add-uri-googleplus
  26. Copy Client ID and Client secret from the Google API console to the corresponding fields in Backendless Console on the Users > Login Providers > Google screen.
    From here:
    client-id-secret-google.zoom80

    To Backendless Console:
    google-setup-backendless.zoom50
Configuring "Login with Github"
  1. Open https://github.com/settings/apps/new
  2. Register a new app.
  3. In "Application name", type in the name of your app.
  4. In Homepage URL use https://api.backendless.com or the homepage URL for your application.
  5. For the Authorization callback URL field copy/paste the Callback URL value from Backendless console. You will find the value in the Users > Login Providers > GitHub screen.
  6. Click Create Github App.
  7. Copy Client ID and Client secret into the corresponding fields in the Users > Login Providers > GitHub screen.
    github-client-key.zoom50
Configuring "Login with LinkedIn"
  1. Open https://www.linkedin.com/developers/apps
  2. Click Create app.
  3. In App name type the name of your app.
  4. Enter LinkedIn page, Privacy policy and App logo:
    linkedin-setup-oauth.zoom50
  5. Click Create app.
  6. Go to Auth
  7. For the Authorization callback URL field copy/paste the Callback URL value from Backendless Console located in Users > Login Providers > LinkedIn
  8. Go to Products
  9. Enable Sign In with LinkedIn:
    linkedin-enable-signin.zoom50
  10. Copy Client ID and Client secret into the corresponding fields in Backendless Console located in the Users > Login Providers > LinkedIn screen.

Get Authorization URL API

This API is used to obtain a URL for the login providers authorization (login) form. The API is used in the OAuth login approach without using login provider's SDK (see Step 1 in Login Without Provider's SDK).

// Login externally authenticated user. The method exchanges the OAuth token (accessToken) to BackendlessUser object. 
// Notice the method is non-blocking - the return value arrives through the AsyncCallback object.
Backendless.UserService.getAuthorizationUrlLink(
                               String providerCode, 
                               Map<String, String> fieldsMappings,
                               List<scope> scope,
                               AsyncCallback<String> responder )

where:

Argument                Description
providerCode name of the login provider as displayed in Backendless Console - see the specific provider screen at Users > Login Providers.
fieldsMappings a mapping of user properties between OAuth provider and Backendless. If the map object is not null, it should contain a mapping between the provider specific property names and the column names in the Users table.
scope a collection of security scopes the client application is requesting the permissions for.
responder a responder object containing success/failure methods which are called when the user is logged in or if an error occurs. If the operation is successful, the callback method provides the BackendlessUser argument representing the currently logged in user.

Return value

The method returns an authorization URL. It should be used to open up the provider's login form.

OAuth2 Login API

This API is used to "exchange" login provider's access token to BackendlessUser, it is used in Step 3 of the Login with Provider SDK flow. The API logs in externally authenticated user to Backendless.


// Login externally authenticated user. The method exchanges the OAuth token (accessToken) to BackendlessUser object. 
// Notice the method is non-blocking - the return value arrives through the AsyncCallback object.
Backendless.UserService.loginWithOAuth2(
          String providerCode, 
          String accessToken, 
          Map<String, String> fieldsMappings,
          AsyncCallback<BackendlessUser> responder, 
          boolean stayLoggedIn )

// same as above but accounts for the guestUser object.
Backendless.UserService.loginWithOAuth2(
          String providerCode,
          String accessToken, 
          BackendlessUser guestUser, 
          Map<String, String> fieldsMappings,
          AsyncCallback<BackendlessUser> responder, 
          boolean stayLoggedIn )

// Login for the OAuth1.0 provider (Twitter)
Backendless.UserService.loginWithOAuth1(
          String providerCode, 
          String authToken,
          String authTokenSecret, 
          Map<String, String> fieldsMappings,
          AsyncCallback<BackendlessUser> responder, 
          boolean stayLoggedIn )

where:

Argument                Description
providerCode name of the login provider as displayed in Backendless Console - see the specific provider screen at Users > Login Providers.
accessToken OAuth access token obtained from the authentication provider as a result of logging in the user.
fieldsMappings Optional. A mapping of user properties between OAuth provider and Backendless. If the map object is not null, it should contain a mapping between the provider specific property names and the column names in the Users table.
responder a responder object containing success/failure methods which are called when the user is logged in or if an error occurs. If the operation is successful, the callback method provides the BackendlessUser argument representing the currently logged in user.
stayLoggedIn requests to store the user's login information so the login form can be skipped next time the user launches the app.

Return value

The API returns a BackendlessUser object for the logged in user.