REST API To Configure SmtpImapConnector (OD-2646)
Deven Phillips opened 2 weeks ago

Hello again. Trying to configure the SmtpImapConnector using the REST API and getting a 401 Unauthorized.

I have confirmed that the username/password works for logging on to the Web UI and has Admin privileges.

Ansible Task:

- name: Configure SMTP Server
  ansible.builtin.uri:
    url: "http://{{ onedev_ip }}:6610/~api/settings/mail-service"
    user: "{{ onedev.admin.username }}"
    password: "{{ onedev.admin.password }}"
    method: POST
    body_format: json
    body: "{{ lookup('ansible.builtin.template', 'email.json.j2') }}"
    # force_basic_auth: true
    status_code: [200, 201, 202]
  tags:
    - onedev

And the JSON Template:

{
  "@type" : "SmtpImapConnector",
  "smtpHost" : "{{ onedev.smtp.host }}",
  "smtpUser" : "{{ onedev.smtp.user }}",
  "smtpPassword": "{{ onedev.smtp.password }}",
  "systemAddress": "{{ onedev.smtp.from }}",
  "inboxPollSetting": null,
  "sslSetting": {
    "@type": "SmtpExplicitSsl",
    "port": 587
  }
}

Which results in:

TASK [onedev : Configure SMTP Server] *******************************************************************************************************************************************
[WARNING]: Module invocation had junk after the JSON data: 
fatal: [cloud-testing]: FAILED! => {"changed": false, "connection": "close", "content_type": "text/plain", "date": "Thu, 25 Dec 2025 19:03:35 GMT", "elapsed": 0, "msg": "Status code was 401 and not [200, 201, 202]: HTTP Error 401: Unauthorized", "redirected": false, "status": 401, "url": "http://172.16.12.33:6610/~api/settings/mail-service"}
  • Robin Shen commented 2 weeks ago

    Tried running with curl and it works:

    curl -u admin:admin [email protected] -H "Content-Type:application/json" http://localhost:6610/~api/settings/mail-service
    
  • Deven Phillips commented 2 weeks ago

    Hmmm... After tweaking and testing some more, I think there is an error in the Ansible uri component and how it handles HTTP Basic auth. This works:

    - name: Configure SMTP Server
      ansible.builtin.uri:
        url: "http://{{ onedev_ip }}:6610/~api/settings/mail-service"
        # user: "{{ onedev.admin.username }}"
        # password: "{{ onedev.admin.password }}"
        headers:
          "Authorization": "Basic {{ (onedev.admin.username + ':' + onedev.admin.password) | b64encode }}"
        #   "Content-Type": "application/json"
        method: POST
        body:
          "@type": "SmtpImapConnector"
          smtpHost: "{{ onedev.smtp.host }}"
          smtpUser: "{{ onedev.smtp.user }}"
          smtpPassword: "{{ onedev.smtp.password }}"
          systemAddress: "{{ onedev.smtp.from }}"
          inboxPollSetting: null
        body_format: json
        # body: "{{ lookup('ansible.builtin.template', 'email.json.j2') }}"
        # force_basic_auth: true
        status_code: [200, 201, 202]
      tags:
        - onedev
    

    Notice that I had to build my own Authorization: BASIC header instead of using the user/pass options from the ansible module. Strange! I will open an issue in the appropriate ansible repo!

  • Deven Phillips commented 2 weeks ago

    Seems that the problem is in Ansible and not OneDev! Thanks @robin

  • Deven Phillips changed state to 'Closed' 2 weeks ago
    Previous Value Current Value
    Open
    Closed
issue 1/1
Type
Question
Priority
Normal
Assignee
Labels
No labels
Issue Votes (0)
Watchers (2)
Reference
OD-2646
Please wait...
Connection lost or session expired, reload to recover
Page is in error, reload to recover