This document focuses on getting the user and session information using an authorization code for apps with a backend server.
With this flow:
token
and refresh_token
from now.gg servers.
The authorization code is a one-time code that your backend server can exchange with now.gg servers to get token
and refresh_token
.
To generate authorization code using your client_id
and sign in the user, refer to the following implementation:
public static final String CODE = "code"; public static final String CLIENT_ID = "your client id"; public static final String ACCOUNT_TYPE = "now.gg"; public static final String HOST_URL = "hostUrl"; private void signIn() { Account account = getNowggAccount(); if (account != null) { Bundle bundle = new Bundle(); bundle.putString("client_id", CLIENT_ID); String authTokenType = CODE; AccountManager.get(getApplicationContext()). getAuthToken(account, authTokenType, bundle, MainActivity.this, new OnTokenAcquired(), null); } else { addNowggAccount(); } } private Account getNowggAccount() { Account[] accounts = AccountManager.get(getApplicationContext()).getAccountsByType(ACCOUNT_TYPE); if (accounts.length > 0) { Log.d(TAG, "getNowggAccount: account found"); // currently only one now.gg account can be added in a system return accounts[0]; } return null; } private void addNowggAccount() { try { Intent intent = new Intent(); intent.setComponent(new ComponentName("gg.now.accounts", "gg.now.accounts.AuthenticatorActivity")); intent.setAction("IAP_ADD_ACCOUNT"); startActivity(intent); } catch (ActivityNotFoundException e) { e.printStackTrace(); } }
Note: This process will generate a callback in OnTokenAcquired()
function.
After the user signs in, you get the requested authorization code in OnTokenAcquired
callback function. To get the authorization code, refer to the following illustration:
private class OnTokenAcquired implements AccountManagerCallback { @Override public void run(AccountManagerFuture result) { try { Bundle bundle = result.getResult(); boolean success = bundle.getBoolean(AccountManager.KEY_BOOLEAN_RESULT, false); if (success) { final String code = bundle.getString(‘authorization_code’); // You are required to send this code to your backend server and call the Generate Tokens API to receive refresh_token/token with this code on app backend server and wait for response from server. } else { // get token failed // error case, developer can show error or show other login mechanisms Log.d(TAG, "run: get token failed " + bundle); } } catch (AuthenticatorException e) { e.printStackTrace(); } catch (IOException exception) { exception.printStackTrace(); } catch (OperationCanceledException e) { e.printStackTrace(); } } }
Once you have the authorization code, send it to your app backend server and use the Generate Tokens API to exchange this code with now.gg servers for a token
and refresh_token
.
token
to call now.gg APIs (User Info and Session Info).refresh_token
to acquire a new token when the token expires.Now that you have the token
, you can use it to fetch the user and the session information.
If your token
has expired, you can generate it with your refresh_token
using the Generate Tokens API.
User Account Service
User Account Service
Document Rev. 1.0