API Authentication Troubles

Gridlok

Cadet
Joined
Feb 24, 2021
Messages
5
I'm having issues Authenticating with the Rest API.

I am running TrueNAS-12.0-U2.1, and everything is working as expected except for the API.

I have tried both Basic authentication and Bearer Authentication, my goal is to use restsharp but I have also tried python and curl and all of them return 401: Unauthorized.

I have tried most methods of authenticating with curl but they generally go something like this.

curl -H 'Accept: application/json' -H "Authorization: Bearer 1-Pe9VwiV5B70yrq8kM8RBLAHBLAHBLAHBLAHalAWIWtr0" http://10.30.163.35/api/v2.0/pool

I have also tried Basic with username:password as Base64.

Does anyone have any idea what I am doing wrong? Or does anyone have an example curl command that works?
 

Gridlok

Cadet
Joined
Feb 24, 2021
Messages
5
I ended up deleting the API token and re-creating it and this started working. Not sure what went wrong the first time.
 

douglasg

Cadet
Joined
Mar 24, 2021
Messages
7
I was having issues with this too and didn't realize that it needed to be "Bearer" instead of "Basic" if you create the API in the Truenas GUI, even though the truenas API calls out Basic specifically. Here is my bash script for posterity/people need help in the future:


Code:
curl -k -X POST -H "Content-Type: application/json" -H "Authorization: Bearer 1-XXXXXXXXXXXXXXXXXXXXXXXX" "https://127.0.0.1/api/v2.0/sharing/nfs" -d '{
  "paths": [
    "/mnt/pool/user/tank"
  ],
  "comment": "",
  "networks": [],
  "hosts": [],
  "alldirs": false,
  "ro": false,
  "quiet": false,
  "maproot_user": "root",
  "maproot_group": "wheel",
  "mapall_user": "",
  "mapall_group": "",
  "security": [],
  "enabled": true
}'
 
Top