Generating Seafile API Token - Requesting Username and Password Even Though Already Inputed

I’m trying to use the following command to get an API token but it keeps telling me that the username and password is required. Does anyone have any suggestions?

command (following the doc – kinda): curl -L -k -d “username=username@email&password=password” -H ‘X-SEAFILE-OTP: xxxxxx’ /api2/auth-token
response:{“detail”:“Method “GET” not allowed.”}

command: curl -L -k -X POST -d “username=username@email&password=password” -H ‘X-SEAFILE-OTP: xxxxxx’ /api2/auth-token
response: {“username”:[“This field is required.”],“password”:[“This field is required.”]}

command: curl -L -k -X POST -d “{“username”:“username@email”, “password”:“password”}” /api2/auth-token
response: {“username”:[“This field is required.”],“password”:[“This field is required.”]}

I ended up using one of the python scripts provided in the git, modifying it to work with python3 and adding the token for 2fa.

Code if anyone else needs it:

#!/usr/bin/env python
"""
Post seafile email/password to obtain auth token.
"""

import urllib
import urllib.parse
import urllib.request
#import urllib2 
#import simplejson as json
import json

url = '<seafile_base_url>/api2/auth-token/'
values = {'username': 'username@email.com',
          'password': 'password'}
#data = urllib.urlencode(values)
data = urllib.parse.urlencode(values).encode("utf-8")
#req = urllib2.Request(url, data)
#response = urllib2.urlopen(req)
req = urllib.request.Request(url, headers={'X-SEAFILE-OTP': '<token>'})
response = urllib.request.urlopen(req, data=data)
the_page = response.read()
token = json.loads(the_page)['token']

print (token)

Some notes:

  • If you’re using python2 or earlier, you should be able use the original code provided, I was using python3 though
  • If you aren’t using 2FA you can remove the header line
1 Like