How-tos
OAuth 2.0 uses short-lived tokens with long-term authorization (refresh tokens) instead of long-lived tokens. A refresh token allows users to stay in your application for an extended period of time without needing to re-enter their username and password. This eliminates the risk of compromising user authentication data.
Set up OAuth 2.0 for authorization:
- via username or email and password
- via social networks
If this option is enabled, user registration and authentication is carried out by calling the
To configure OAuth 2.0 authorization:
- Set up OAuth 2.0 authentication for Login project in your Publisher Account.
- Initialize the library.
Set up OAuth 2.0 authentication for Login project in your Publisher Account
- Go to your Publisher Account.
- Click Open in the Login block and go to Login projects.
- Click Open and set up in the Login project block.
- Go to General settings > Authorization.
- Click Connect in the OAuth 2.0 authentication block.
- Specify OAuth 2.0 redirect URIs and click Connect.
- Copy and save the Client ID.
Initialize the library
To initialize the library, add the following line to your Android project source code, specifying the following parameters:
login-project-id — Login ID found in Publisher Account > Login settings > Login ID.oauth2-client-id — Client ID received when setting up OAuth 2.0 in Publisher Account.
- kotlin
val loginConfig = LoginConfig.OauthBuilder()
.setProjectId("login-project-id")
.setOauthClientId("oauth2-client-id")
.build()
XLogin.init(applicationContext, loginConfig)
The following methods are implemented in Login Android SDK to work with refresh tokens:
XLogin.refreshToken — refreshes the token.XLogin.getToken — returns the current token.XLogin.isTokenExpired — returnstrue if the token is expired.
Native authentication allows players to log in to your application via installed applications that use social network accounts. Currently, Android SDK has implemented native authentication via the following social networks:
To configure native authentication:
- For native authentication via Facebook, create your Facebook developer account and a new app.
- Set up the application page in your Facebook developer account.
- For native authentication via Google, set up the project in Google API Console.
- Set up social networks for the Login project in your Publisher Account.
- Open
build.gradle file of your application and add the following lines to the dependencies section where<version_number> is the required version of the Android SDK:
- groovy
implementation 'com.xsolla.android:login-facebook:<version_number>'
implementation 'com.xsolla.android:login-google:<version_number>'
- Initialize the Login library with the following parameters:
facebook_id — App ID from the Facebook developer account.google_id — Client ID for web application from the Google API Console.
- kotlin
val loginConfig = LoginConfig.JwtBuilder()
.setProjectId("login-project-id")
.setCallbackUrl("your-callback-url")
.setSocialConfig(XLogin.SocialConfig(facebookAppId = "facebook_id", googleServerId = "google_id"))
.build()
XLogin.init(applicationContext, loginConfig)
// OR
val loginConfig = LoginConfig.OauthBuilder()
.setProjectId("login-project-id")
.setOauthClientId("oauth2-client-id")
.setCallbackUrl("your-callback-url")
.setSocialConfig(XLogin.SocialConfig(facebookAppId = "facebook_id", googleServerId = "google_id"))
.build()
XLogin.init(applicationContext, loginConfig)
Set up application page in your Facebook developer account
- Go to project settings in the Facebook developer account.
- Go to Settings > Basic.
- Click Add Platform and select Android.
- Specify the Package name of your Android app for the Google Play Package Name.
- Specify a fully qualified class name of the default Activity for Class Name.
- Generate a hash key and specify it as a Key Hashes.
- Click Save Changes.
For further native authentication configuration, you will need App ID and App Secret found in project settings in Settings > Basic section.
Set up project in Google API Console
- Go to Google API Console.
- Click New Project.
- Specify Project name and Location and click Save.
- Go to the created project and click OAuth consent screen in the side menu.
- Select External option and click Create.
- Specify the necessary parameters and click Save.
- Click Credentials in the side menu.
- Create an OAuth 2.0 client for your Android app:
- Click Create credentials and select OAuth client ID.
- Specify Android as the Application type.
- Specify Name.
- Specify Package name of your Android app as a Package name.
- Get the SHA-key.
- Specify SHA-key generated in the previous step SHA-key as a SHA-1 certificate fingerprint.
- Click Create.
- Click OK.
- Create an OAuth 2.0 client for the web application:
- Click Create credentials and select OAuth client ID.
- Specify Web application as the Application type.
- Specify Name.
- Click Add URI in the Authorized redirect URIs section and specify https://login.xsolla.com/api/social/oauth2/callback URI.
- Click Create.
- Click OK.
For further native authentication configuration, you will need Client ID and Client Secret found in settings of the Client ID for the web application.
Set up social networks for Login project in your Xsolla Publisher Account
- Go to your Publisher Account.
- Click Open in the Login block and go to Login projects.
- Click Open and set up in the Login project block.
- Go to Social connections.
- To set up authentication via Facebook:
- Click Edit in the Facebook block and change status to Disconnected.
- Specify the App ID from the Facebook developer account as Application ID.
- Specify App Secret from the Facebook developer account as Application Secret.
- Click Connect.
- To set up authentication via Google:
- Click Edit in the Google block and change status to Disconnected.
- Specify the Client ID for a web application from the Google API Console as Application ID.
- Specify the Client Secret for a web application from the Google API Console as Application Secret.
- Click Connect.
Token invalidation allows for improved security of user authentication data in your application. If the option is enabled, a new token replaces the old one that becomes invalid every time the user authenticates.
When using the Login Unity SDK, invalidation of the existing token and generation of a new one is made by calling
To use token invalidation in your Android project, you need to pass the
You can use attributes to manage additional information about the users of your application. The user attribute is a key-value pair. The Login SDK supports the following types of attributes:
- User-editable attributes. The values for this type of attributes are entered by a user or specified according to the in-game logics on the client side. For example: the name and character stats, game difficulty level, etc.
- Read-only attributes. The values for this type of attributes are entered and edited on the server side of your application. We recommend you use them for configuration of game character stats or user parameters that shouldn’t be changed a lot. For example: use them for the chance to get a bonus, game character key parameters, user categories, etc.
To manage user attributes, use the following SDK methods:
XLogin.getUsersAttributesFromClient — gets a list of attributes for a specific user.Get user’s attributes from client andGet user’s read-only attributes from client API methods are used alongside the SDK method.XLogin.updateUsersAttributesFromClient — updates and creates attributes of a specific user. TheUpdate user’s attributes from client API method is used alongside the SDK method.
When working with read-only attributes, you can use a server token or a Publisher Account token for authorization.
To get the Publisher Account token:
- Enter your Publisher Account.
- Go to the cookie files viewpage via the developers’ tools in your browser.
- Copy the value from the pa-v4-token parameter and paste it to the
Authorization header.
Users can configure the following data from a user account:
- public profile data:
- avatar
- nickname
- personal user data:
- name and surname
- birthdate
- gender
- phone number
You can manage the user account via the API methods. The Login Android SDK has the following methods implemented:
XLogin.getCurrentUserDetails — gets the user’s data.XLogin.updateCurrentUserDetails — updates the user’s data.
XLogin.getCurrentUserEmail — gets the user’s email address.XLogin.getCurrentUserPhone — gets the user’s phone number.XLogin.updateCurrentUserPhone — updates the user’s phone number.XLogin.deleteCurrentUserPhone — deletes the user’s phone number.XLogin.uploadCurrentUserAvatar — changes the user’s avatar.XLogin.deleteCurrentUserAvatar — deletes the user’s avatar.
You can see the examples of the method calls in the demo project.
The friend system allows your users to find each other and set up social connections. The SDK supports the following functionality:
- search by nickname
- get the list of friends from social networks
- manage the friend list, send invitations, add and remove friends, block users, etc.
- manage personal data via the user account
To implement the friend system, you should have the user account functionality in your application. Methods of working with this system use the following parameters from the user account:
- user ID
- avatar
- nickname
Users should specify the nickname to ensure the friend system works correctly. Implement the following nickname specification logics:
- Use the name that was used for registration via login and password as a nickname.
- Implement the nickname request during social authentication or platform accounts.
SDK methods
The Login Android SDK has the following methods of working with the friend system:
XLogin.getCurrentUserFriends — updates the locally cached user friends data. TheGet friends API method is used alongside the SDK method.
XLogin.updateCurrentUserFriend — updates the status of the user’s social connections. TheUpdate friends API method is used alongside the SDK method. The social connections status is affected by the following actions:- send or cancel a friend request
- accept or decline a friend request
- delete the user from the friend list
- block or unblock the user
XLogin.updateCurrentUserFriend — updates the status of the user’s social connections. TheUpdate friends API method is used alongside the SDK method. The social connections status is affected by the following actions:- send or cancel a friend request
- accept or decline a friend request
- delete the user from the friend list
- block or unblock the user
XLogin.getSocialFriends — updates the locally cached data of user’s friends from a social network. TheGet user’s friends API method is used alongside the SDK method.
XLogin.getUserPublicInfo — gets data from the user’s public profile. TheGet user public profile API method is used alongside the SDK method.
XLogin.searchUsersByNickname — searches for the user by nickname. TheSearch users by nickname API method is used alongside the SDK method.
XLogin.startSocialAccountLinking — generates an intent to open a social network authorization window to link an account to a user account. TheLink social network to user's account API method is used alongside the SDK method.
Implementing a friends system for social networks
To let users interact with friends from social networks in your application, configure the storage of friend data on the Xsolla side:
- Go to your Publisher Account.
- Click Open in the Login block and go to Login projects.
- Click Open and set up in the Login project block.
- Go to General settings > Authentication.
- Set the Store friends from social networks toggle to On.
- Save changes.
To make friends from the social network available to the player in the application, implement the following logic in your application:
- Link a social network to a player's account using the
XLogin.createSocialAccountLinkingIntent method.
Example:
- kotlin
startActivityForResult(
XLogin.createSocialAccountLinkingIntent(context, socialNetwork),
RC_LINKING
)
- Update your friends list using the
XLogin.updateSocialFriends method. TheUpdate user’s friend API method is used alongside the SDK method. - Get a list of friends from a linked social network using the
XLogin.getSocialFriends method. TheGet user’s friends API method is used alongside the SDK method.
You can return users to your application after they pay for in-game purchases via an external browser. To do this, set up deep links:
- Go to Xsolla Publisher Account.
- Go to your project and click Open in the Pay Station block.
- Go to Settings.
- Specify the required parameters in the Redirect policy section and click Save.
- Add the Return URL configured in Publisher Account to the strings.xml file of application module as shown in the example. An example is given for a Return URL with the value
myapp://example.com/payment .
- xml
<string name="xsolla_payments_redirect_scheme">myapp</string>
<string name="xsolla_payments_redirect_host">example.com</string>
<string name="xsolla_payments_redirect_path_prefix">/payment</string>
To get new users to your application and increase sales, you can implement coupon promotions. When redeeming a coupon, the user may receive one of the following rewards:
- virtual currency package
- game key
- virtual item
For details on the features and limitations of coupon promotions, see the In-Game Store guide.
To work with coupon promotions:
- Complete the settings in Publisher Account following to the instructions for setting up promotional campaigns with coupons.
- Implement in-game logic using the following SDK methods:
XStore.getCouponRewardsByCode — gets a list of items that can be credited to the user when the coupon is redeemed. TheGet coupon rewards API method is used alongside the SDK method.XStore.redeemCoupon — redeems the coupon code and rewards the user. TheRedeem coupon code API method is used alongside the SDK method.
To get new users to your application and increase sales, you can implement a campaign with promo codes. When redeeming a promo code, the user may receive one or more of the following rewards:
- discount that applies to the user’s cart
- bonus items:
- virtual currency package
- game key
- virtual item including a bundle or nonrenewing subscription
For details on the features and limitations of campaigns with promo codes, see the In-Game Store guide.
To work with promo codes:
- Complete the settings in Publisher Account by following the instructions for setting up a campaign with promo codes.
- Implement in-game logic using the following SDK methods:
XStore.getPromocodeRewardsByCode — gets promo code rewards. Allows users to choose one of many items as a bonus. TheGet promo code reward API endpoint is used alongside the SDK method.XStore.redeemPromocode — redeems promo code. After redeeming a promo code, the user gets free items and/or the price of the cart is decreased. TheRedeem promo code API endpoint is used alongside the SDK method.
To get new users to your application and increase sales, you can sell sets of items as bundles for less than the cost of their content.
A bundle may include:
- virtual currency (including the platform-dependent currency)
- package of virtual currency
- game keys for preselected DRMs
- virtual items including nonrenewing subscriptions
- bundles
For details on the features and limitations of bundles, see the In-Game Store guide.
To work with bundles:
- Complete the settings in Publisher Account by following the instructions for setting up a bundle.
- Implement in-game logic using the following SDK methods:
XStore.getBundleList — gets a list of bundles for building a catalog. TheGet list of bundles API endpoint is used alongside the SDK method.XStore.getBundle — gets a bundle for the specified SKU. TheGet specified bundle API endpoint is used alongside the SDK method.
You can integrate Store Android SDK, Payments Android SDK and Inventory Android SDK with your own login system. To do so, you need to implement user authentication in your application via Pay Station access token. See the authentication algorithm in the In-Game Store documentation.
Implement the following logic in your application:
- On the back-end of your application, implement a method to receive the Pay Station access token using an HTTP POST request. Xsolla API uses basic HTTP authentication. Enter your Merchant ID as a username and API key as a password.
HTTP request:
- http
POST https://api.xsolla.com/merchant/v2/merchants/{merchant_id}/token
Learn more about obtaining a token in the Pay Station documentation.
- Pass the received Pay Station access token to the
XStore.init method. - Implement the logic of receiving a new Pay Station access token after its expiration. It is recommended that you get a new token in the background mode, so the user doesn’t have to log in to the application again.
Implement the sale of virtual items and virtual currency outside the game via the in-game store site. The purchased items and currency will be available in the player's inventory.
To integrate your store site with the SDK:
- Create a store site.
- Specify the site address of the in-game store in the application code.
- Implement the logic for your application to work with the store site.
Create a store site
You can create a store site with Site Builder by following the instructions for connecting a store on the site.
If you want to connect a store site that was created using a different solution, follow the instructions for creating a store.
Recommendations for the logic for your application to work with the store site
Follow these recommendations when developing your application:
- Synchronize the fields of user registration and authorization in your application and in the Login widget on the store site:
- If a user email and password are used for registration and authorization, set the username equal to the email in the application.
- If you use a username, user email and password for registration and authorization, contact your Account Manager to change the Login widget fields.
- When navigating to the store site from the application client, implement pass-through user authorization. To do this, the user token must be passed in the URL parameters. An example of authorization is shown in the demo app.