通过 plsql 脚本创建 apex 用户

create apex user via plsql script

运行 来自 https://www.talkapex.com/2012/08/how-to-create-apex-session-in-plsql 的脚本并得到错误

[Error] Execution (1: 1): ORA-20987: APEX - Application ID and current security group ID are not consistent. - Contact your application administrator.
Details about this incident are available via debug id "78243".
ORA-06512: at "APEX_190100.WWV_FLOW_ERROR", line 1381
ORA-06512: at "APEX_190100.WWV_FLOW_ERROR", line 1416
ORA-06512: at "APEX_190100.WWV_FLOW_CUSTOM_AUTH_STD", line 724
ORA-06512: at "APEX_190100.WWV_FLOW_CUSTOM_AUTH_STD", line 588
ORA-06512: at "APEX_190100.HTMLDB_CUSTOM_AUTH", line 260
ORA-06512: at "USER.SP_CREATE_APEX_SESSION", line 30
ORA-06512: at line 2

引发的错误
apex_custom_auth.post_login(
    p_uname => p_app_user,
    p_session_id => APEX_CUSTOM_AUTH.GET_NEXT_SESSION_ID,
    p_app_page => apex_application.g_flow_id||':'||p_app_page_id);

如何正确创建需要wokrspace的用户? Apex 版本 19.1

并创建用户

BEGIN
    FOR C1 IN (SELECT WORKSPACE_ID
                 FROM APEX_APPLICATIONS
                WHERE APPLICATION_ID = 4050)
    LOOP
        APEX_UTIL.SET_SECURITY_GROUP_ID (
            P_SECURITY_GROUP_ID   => C1.WORKSPACE_ID);
    END LOOP;
    
    APEX_UTIL.CREATE_USER (
        P_USER_NAME                 => 'BOB',
        P_EMAIL_ADDRESS             => 'bob@bob.com',
        P_DEFAULT_SCHEMA            => 'MY_SPACE',
        P_ALLOW_ACCESS_TO_SCHEMAS   => 'MY_SPACE',
        P_WEB_PASSWORD              => 'change_me',
        P_DEVELOPER_PRIVS           =>
            'ADMIN:CREATE:DATA_LOADER:EDIT:HELP:MONITOR:SQL'); -- workspace administrator

    COMMIT;
END;

我有同样的错误

[Error] Execution (2: 1): ORA-20987: APEX - Security Group ID (your workspace identity) is invalid. - Contact your application administrator.

declare
    l_workspace_id      number;
begin
    l_workspace_id := apex_util.find_security_group_id (p_workspace => 'INTERNAL');
    apex_util.set_security_group_id (p_security_group_id => l_workspace_id);    
    apex_util.create_user(
        p_user_name    => 'NEWUSER1',
        p_web_password => 'secret99');
    commit;
end;

Error at line 1 ORA-20001: Package variable g_security_group_id must be set. ORA-06512: at "APEX_190100.WWV_FLOW_API", line 1828 ORA-06512: at "APEX_190100.WWV_FLOW_API", line 1863 ORA-06512: at "APEX_190100.WWV_FLOW_FND_USER_INT", line 1977 ORA-06512: at "APEX_190100.HTMLDB_UTIL", line 1452 ORA-06512: at line 6

SELECT WORKSPACE,WORKSPACE_DISPLAY_NAME, OWNER, APPLICATION_GROUP_ID  FROM APEX_APPLICATIONS WHERE WORKSPACE = 'INTERNAL' AND APPLICATION_ID = 4050;

INTERNAL >INTERNAL
APEX_190100
9.1051E+16 1 row selected.

好的,现在这更有意义了。您正在尝试创建用户,但在创建用户之前尝试创建会话时失败。您不需要会话来创建用户,您只需要设置安全组。这可以使用 APEX_UTIL.SET_SECURITY_GROUP_ID 来完成。我在我的环境中执行了这段代码,通过 sqldeveloper 连接:

DECLARE
    l_workspace_id      number;
BEGIN
    l_workspace_id := apex_util.find_security_group_id (p_workspace => '<myworkspacename>');
    apex_util.set_security_group_id (p_security_group_id => l_workspace_id);    
    APEX_UTIL.CREATE_USER(
        p_user_name    => 'NEWUSER1',
        p_web_password => 'secret99');
    COMMIT;
END;
/

并且用户的创建没有错误。