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:
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 use authenticate the users through the provider. A typical authentication flow is:
- Use login provider's SDK or API to login the user.
- Once the user is logged in, you application receives an OAuth token.
- Use the OAuth token with the Backendless API to "exchange" the token and receive a reference to Backendless user. The Backendless user is a logical representation of the user authenticated through the login provider. The API to login an externally authenticated user is documented below in this section of the guide.
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"
- Navigate to https://developers.facebook.com/
- 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.
- Add Facebook Login to your app
- 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).
- Click Add platform and select Android.
- Enter your Class Name, Package Name and Key Hashes.
- Click Save Changes.
- Click the Settings tab on the Facebook Product section.
- Scroll down to the Client OAuth Settings block and enter
https://api.backendless.cominto the Valid OAuth redirect URIs field.
- 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):
- Scroll down to the bottom of the page and click Save Changes.
Configuring "Login with Google"
Adding Google API and generating client ID¶
- Navigate to https://console.developers.google.com
Choose an existing project or create a new one.
Select the Google+ API:
- Once the API is enabled, click the Create Credentials button. If you do not see the button, click the Credentials menu item on the left and then click the Configure Consent Screen button.
- Complete the form on the OAuth consent screen:
- Return to the Credentials and click +Create Credentials > Create OAuth client ID.
- Select Android.
Copy the command starting with keytool...using the copy icon:
Open a command prompt/terminal window. Run the
keytoolcommand, but make sure to set the value for the
-keystoreargument. 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
- 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.
- Enter the package name which you will use in your application and click Create client ID.
Generating config file for client project¶
In this step of the setup you need to navigate to https://console.firebase.google.com/ and add/create a Firebase project. Follow Google's instructions for configuring the project, downloading google-services.json and adding the file to your Android Studio project.
Adding Configuration in Backendless¶
- Return to https://console.developers.google.com
- Select the project and click Credentials.
- The credentials should now include a "Web client" which is automatically generated by Google.
Configuring "Login with Github"
- Open https://github.com/settings/apps/new
- Register a new app.
- In "Application name", type in the name of your app.
- In Homepage URL use
https://api.backendless.comor the homepage URL for your application.
- 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.
- Click Create Github App.
- Copy Client ID and Client secret into the corresponding fields in the Users > Login Providers > GitHub screen.
Configuring "Login with LinkedIn"
- Open https://www.linkedin.com/developers/apps
- Click Create app.
- In App name type the name of your app.
- Click Create app.
- Go to Auth
- For the Authorization callback URL field copy/paste the Callback URL value from Backendless Console located in Users > Login Providers > LinkedIn
- Go to Products
- Enable Sign In with LinkedIn:
- Copy Client ID and Client secret into the corresponding fields in Backendless Console located in the Users > Login Providers > LinkedIn screen.
OAuth Login API¶
There are two available APIs: one supporting the OAuth2 login providers and the other is for the older version of OAuth (which at this point is only Twitter).
// 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 )
||name of the login provider as displayed in Backendless Console - see the specific provider screen at Users > Login Providers.|
||OAuth access token obtained from the authentication provider as a result of logging in the user.|
||Optional. A mapping of user properties between OAuth provider and Backendless. If the map object is not
||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
||requests to store the user's login information so the login form can be skipped next time the user launches the app.|