How to access ejabberd admin api via HTTP?

2019-02-14 09:06发布

问题:

I read about the doc https://docs.ejabberd.im/admin/api/

And I wonder how can I access those API via HTTP. I tried to access but it always 404.

回答1:

In your ejabberd.yml you can find a configuration like this

- 
port: 5280
module: ejabberd_http
request_handlers:
  "/websocket": ejabberd_http_ws

Enable api and oauth,

- 
port: 5280
module: ejabberd_http
request_handlers:
  "/websocket": ejabberd_http_ws
  "/api": mod_http_api
  "/oauth": ejabberd_oauth

We are enabling api to have access from other programming languages(JSON request and response)

We are enabling oauth to raise an api request and get response(Without which if we access /api we will get only 401-unauthorized error)

Once this configuration in done, give a http request in your browser. URL must be

http://localhost:5280/oauth/authorization_token?response_type=token&client_id=Client1&scope=get_roster+connected_users

get_roster and connected_users are API end points Clinet1 is some name that you can give response_type must be always token

Once you hit it it will render a screen as this,

Enter your admin details and click on accept

Then in the url you will find a change something like this

http://localhost:5280/oauth/authorization_token?access_token=Ra9W9aRgeoUgIpN0P68SIGDaatDIVcgB&token_type=bearer&expires_in=3600&scope=get_roster%20connected_users&state=

You can see a access token there copyit and do a curl to get the connected users. (Type this command in your terminal)

curl -v POST -H "X-Admin: true" -H "Authorization: Bearer Ra9W9aRgeoUgIpN0P68SIGDaatDIVcgB" http://localhost:5280/api/connected_users -d '[]'

You will get a Json response with all connected users.

This is how you should make API calls in ejabberd. Hope this helps :)



回答2:

enable ejabberd_xmlrpc in ejabberd.yml file uncomment the following lines-

## To handle XML-RPC requests that provide admin credentials:
  ##
  ## - 
  ##   port: 4560
  ##   module: ejabberd_xmlrpc
  ##   maxsessions: 10
  ##   timeout: 5000
  ##   access_commands:
  ##     xmlrpc:
  ##       commands: all
  ##   options: []

All XMLRPC are send to the following URL: http://host:4560/. for more detail check here