/**
   * Does all the necessary username/password authentication stuff in one go
   *
   * <p>Generates a new valid TokenAuthorisation
   *
   * <p>Validates the Token via username/password
   *
   * <p>requests a new session id with the validated TokenAuthorisation and returns a new
   * TokenSession which one may want to transform into SessionToken for APO calls that require a
   * authorized user.
   *
   * @return validated TokenSession
   * @throws info.movito.themoviedbapi.tools.MovieDbException if the login failed
   */
  public TokenSession getSessionLogin(String username, String password) {
    TokenAuthorisation authToken = getAuthorisationToken();

    if (!authToken.getSuccess()) {
      throw new MovieDbException("Authorisation token was not successful!");
    }

    TokenAuthorisation loginToken = getLoginToken(authToken, username, password);

    if (!loginToken.getSuccess()) {
      throw new MovieDbException("User authentication failed:" + loginToken.toString());
    }

    return getSessionToken(loginToken);
  }
  /**
   * This method is used to generate a session id for user based authentication.
   *
   * <p>A session id is required in order to use any of the write methods.
   *
   * @param token
   */
  public TokenSession getSessionToken(TokenAuthorisation token) {
    ApiUrl apiUrl = new ApiUrl(TMDB_METHOD_AUTH, "session/new");

    if (!token.getSuccess()) {
      logger.warn("Authorisation token was not successful!");
      throw new MovieDbException("Authorisation token was not successful!");
    }

    apiUrl.addParam(PARAM_REQUEST_TOKEN, token.getRequestToken());

    return mapJsonResult(apiUrl, TokenSession.class);
  }