/**
   * 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);
  }
  /**
   * 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);
  }
  /**
   * Try to validate TokenAuthorisation with username and password.
   *
   * @param token A TokenAuthorisation previously generated by getAuthorisationToken
   * @param user username
   * @param pwd password
   * @return The validated TokenAuthorisation. The same as input with getSuccess()==true
   */
  public TokenAuthorisation getLoginToken(TokenAuthorisation token, String user, String pwd) {
    ApiUrl apiUrl = new ApiUrl(TMDB_METHOD_AUTH, "token/validate_with_login");

    apiUrl.addParam(PARAM_REQUEST_TOKEN, token.getRequestToken());
    apiUrl.addParam("username", user);
    apiUrl.addParam("password", pwd);

    return mapJsonResult(apiUrl, TokenAuthorisation.class);
  }