Login by Manually Specifying an Access Token

In this method, your application retrieves the access token from the SDK and stores it in the application storage. The application can later login with this token when the process is rebooted.

The access token can be retrieved in two ways. One way is by calling the getAccessToken method. Another way is to get a bundle by getAccessTokenBundle method and get token value from the bundle by access_token key. In both cases, you have to call the method while the user is logged in. The following sample code shows the both ways:

// Assume that a user has logged in.

// Method #1: Get an access token with the getAccessToken() method.
String accessToken = KiiUser.getCurrentUser().getAccessToken();

// Method #2: Get an access token with the getAccessTokenBundle() method.
Bundle b = KiiUser.getCurrentUser().getAccessTokenBundle();
accessToken = b.getString("access_token");

// Securely store the access token in the storage with your own function.
storeToken(accessToken);

Here is the sample code for authenticating a user with an access token. By calling the loginWithAccessToken method with the access token, your user will be authenticated again.

  • // Get an access token from the storage with your own function.
    String accessToken = getStoredToken();
    
    try {
      // Authenticate a user with the access token.
      KiiUser.loginWithToken(accessToken);
    } catch (IOException e) {
      // Handle the error.
    } catch (AppException e) {
      // Handle the error.
    }
  • // Get an access token from the storage with your own function.
    String accessToken = getStoredToken();
    
    // Authenticate a user with the access token.
    KiiUser.loginWithToken(new KiiUserCallBack() {
      @Override
      public void onLoginCompleted(int token, KiiUser user, Exception exception) {
        if (exception != null) {
          // Handle the error.
          return;
        }
      }
    }, accessToken);

Please make sure to store an access token in a secure place. It should not be accessible by other applications. If a malicious application gets the access token, it will gain privileges to access Kii Cloud as the owner of this token.

In this approach, your mobile app controls the access token. This allows your mobile app to manage multiple user logins by keeping the access tokens for them and by switching to the appropriate access token. The client SDK can only hold one access token at a time, so the mobile app needs to switch users).

Note that you cannot use the refresh token if you manually specify an access token for login. This is because this login method only restores the access token. In order to user the refresh token, you need to use the login method described in Login with the Auto-Saved Credentials.