首页 > 解决方案 > 命令需要以管理员权限 Ejabberd Rest Api 运行?

问题描述

Ejabberdejabberd 16.09. 要配置它,我只是关注 ejabberd 文档(https://docs.ejabberd.im/developer/ejabberd-api/simple-configuration/)。所以当我要从邮递员那里点击http://localhost:5280/api/change_password urlbody:

{
      "user": "123",
      "host": "TT_CPU_076",
      "newpass": "blank"
    }

header: Content-Type: application/json.收到如下错误:

{
    "status": "error",
    "code": 31,
    "message": "Command need to be run with admin priviledge."
}

我的 ejabberd.yml 文件配置如下:

port: 5280
    module: ejabberd_http
    ip: "127.0.0.1"
    request_handlers:
      "/websocket": ejabberd_http_ws
      "/api": mod_http_api
    ##  "/pub/archive": mod_http_fileserver
    web_admin: true
    http_bind: true
    ## register: true
    captcha: false
api_permissions:
  "API used from localhost allows all calls":
    - who:
      - ip: "127.0.0.1/8"
    - what:
      - "*"
      - "!stop"
      - "!start"

任何使用 ejabberd 实现 Rest Api 配置的解决方案都将不胜感激。

标签: restejabberdejabberd-auth

解决方案


我无法用最近的 ejabberd 18.04 重现该问题。我在 ejabberd.yml 中设置:

hosts:
  - "localhost"
  - "TT_CPU_076"

api_permissions:
  "public commands":
    who:
      - ip: "127.0.0.1/8"
    what:
      - "*"
      - "status"
      - "connected_users_number"

然后我注册该帐户:

$ ejabberdctl register 123 TT_CPU_076 pass11
User 123@TT_CPU_076 successfully registered

我编写了一个简单的 API 客户端:

<?php
$url='localhost:5280/api/change_password/';
$info=array("user"=>"123",
            "host"=>"TT_CPU_076",
            "newpass"=>"mypass22"
           );
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($info));
$output=curl_exec($ch);
curl_close($ch);
print_r($output);
?>

然后我执行它:

$ php test.php 
0

这是在 ejabberd 中登录的:

11:39:18.646 [info] API call change_password 
  [{<<"user">>,<<"123">>},
   {<<"host">>,<<"TT_CPU_076">>},
   {<<"newpass">>,<<"mypass22">>}]
  from ::ffff:127.0.0.1:46778

最后,我检查了数据库,密码已更改


推荐阅读